I have a form inside javascripts/fixtures/form.html
. I am checking for whether my field is empty
or contains less than eight character
or has special characters
.
it("Username should not be empty", function(){
spyOn($("#name"), "val").and.returnValue("Java");
var result = $("#name").val();
expect(result).toEqual("Java");
});
it("Username should be of the length greater than eight", function(){
spyOn($("#name"), "val").and.returnValue("Java");
var result = $("#name").val();
expect(result).toEqual("Java");
});
it("Username should not contain special characters", function(){
spyOn($("#name"), "val").and.returnValue("Java");
var result = $("#name").val();
expect(result).toEqual("Java");
});
I am not sure whether this is the right way to do unit testing. Can anyone guide me on how to do unit testing for an input field on these three cases.
I have a form inside javascripts/fixtures/form.html
. I am checking for whether my field is empty
or contains less than eight character
or has special characters
.
it("Username should not be empty", function(){
spyOn($("#name"), "val").and.returnValue("Java");
var result = $("#name").val();
expect(result).toEqual("Java");
});
it("Username should be of the length greater than eight", function(){
spyOn($("#name"), "val").and.returnValue("Java");
var result = $("#name").val();
expect(result).toEqual("Java");
});
it("Username should not contain special characters", function(){
spyOn($("#name"), "val").and.returnValue("Java");
var result = $("#name").val();
expect(result).toEqual("Java");
});
I am not sure whether this is the right way to do unit testing. Can anyone guide me on how to do unit testing for an input field on these three cases.
Share Improve this question edited May 20, 2014 at 13:02 theJava asked May 19, 2014 at 10:17 theJavatheJava 15.1k48 gold badges134 silver badges174 bronze badges1 Answer
Reset to default 2You can not check the input data directly with this:
var result = $("#name").val();
expect(result).toEqual("Java");
To check your input field value, you can load/set the fixtures and can assign the value to your fixtures and can read the fixtures' input data then you can check with .toEqual("") or something else.
You can refer this code to check the input field value:
var value = 'some value';
var differentValue = 'different value';
beforeEach(function () {
setFixtures($('<input id="sandbox" type="text" />').val(value));
});
it("should pass if value matches expectation", function () {
expect($('#sandbox')).toHaveValue(value);
expect($('#sandbox').get(0)).toHaveValue(value);
});
I hope this url will help you bit more to understand the fixtures: https://github./velesin/jasmine-jquery/#cross-domain-policy-problems-under-chrome
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745133963a4613108.html
评论列表(0条)