简体   繁体   English

使用jasmine对输入字段进行单元测试

[英]unit testing for an input field using jasmine

I have a form inside javascripts/fixtures/form.html . 我在javascripts/fixtures/form.html有一个表单。 I am checking for whether my field is empty or contains less than eight character or has special characters . 我正在检查我的字段是否为empty或包含less than eight character或具有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. 任何人都可以指导我如何在这三种情况下对输入字段进行单元测试。

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. 要检查输入字段值 ,您可以加载/设置灯具并将值分配给灯具,并可以读取灯具的输入数据,然后您可以使用.toEqual(“”)或其他内容进行检查。

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.com/velesin/jasmine-jquery/#cross-domain-policy-problems-under-chrome 我希望这个网址可以帮助你更多地理解这些工具: https//github.com/velesin/jasmine-jquery/#cross-domain-policy-problems-under-chrome

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM