javascript - unit testing for an input field using jasmine - Stack Overflow

I have a form inside javascriptsfixturesform.html. I am checking for whether my field is empty or con

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 badges
Add a ment  | 

1 Answer 1

Reset to default 2

You 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

相关推荐

  • javascript - unit testing for an input field using jasmine - Stack Overflow

    I have a form inside javascriptsfixturesform.html. I am checking for whether my field is empty or con

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信