如果变量未定义,则进行Jasmine测试

时间:2017-03-10 10:25:38

标签: javascript jasmine

我想用Jasmine测试我的代码。我想测试的代码是:

window.document.cookie = 'user_input=;path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT';

var user_input = $('.selector').val();

if (typeof user_input !== "undefined") {
    var date = new Date;

    user_input = user_input.replace(/(\r\n|\n|\r)/gm, ' ');
    date.setDate(date.getDate() + 1);
    window.document.cookie = "user_input=" + user_input + ';path=/;expires=' + date.toGMTString();
}

我的测试是:

describe('get_user_input', () => {
    it('should update the cookie with users input if the input is NOT undefined', () => {
        window.document.cookie = 'test_case=';
        var user_input = 'test';
        expect(window.document.cookie).toEqual('test_case=test');
    });
});

然而这不起作用,我对测试很新,所以任何帮助将不胜感激!谢谢!

2 个答案:

答案 0 :(得分:1)

您的函数从DOM获取元素。所以在你的测试中你必须在DOM中创建这样的元素,你的函数将使用它。

describe('get_user_input', () => {
    beforeEach(() => {
        window.document.cookie = 'test_case=';
        $("<div class='selector'>test</div>").appendTo("body");
        yourFunction();
    });

    it('should update the cookie with users input if the input is NOT undefined', () => {
        expect(window.document.cookie).toEqual('test_case=test');
    });
});

更优选的解决方案是从您的函数中删除DOM操作,并使此函数易于测试且耦合较少。

function yourFunction(user_input) {
    window.document.cookie = 'user_input=;path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT';

    if (typeof user_input !== "undefined") {
        var date = new Date;

        user_input = user_input.replace(/(\r\n|\n|\r)/gm, ' ');
        date.setDate(date.getDate() + 1);
        window.document.cookie = "user_input=" + user_input + ';path=/;expires=' + date.toGMTString();
    }
}

并测试它:

describe('get_user_input', () => {
    let user_input;

    beforeEach(() => {
        window.document.cookie = 'test_case=';
        user_input = "test";
        yourFunction(user_input);
    });

    it('should update the cookie with users input if the input is NOT undefined', () => {
        expect(window.document.cookie).toEqual(`test_case=${user_input}`);
    });
});

甚至更多 - 将window.document.cookie移出函数并返回计算值。然后将此返回值设置为应用中其他位置的window.document.cookie

function yourFunction(cookie, user_input) {
    let res = 'user_input=;path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT';

    if (typeof user_input !== "undefined") {
        var date = new Date;

        user_input = user_input.replace(/(\r\n|\n|\r)/gm, ' ');
        date.setDate(date.getDate() + 1);
        res = "user_input=" + user_input + ';path=/;expires=' + date.toGMTString();
    }
    return res;
}

describe('get_user_input', () => {
    let cookie;
    let user_input;
    let output;

    beforeEach(() => {
        cookie = 'test_case=';
        user_input = "test";
        output = yourFunction(cookie, user_input);
    });

    it('should update the cookie with users input if the input is NOT undefined', () => {
        expect(output).toEqual(`test_case=${user_input}`);
    });
});

让您的功能尽可能简单。单一责任岩石!

答案 1 :(得分:-1)

请参阅此简单示例Jasmin landpage

describe("A suite is just a function", function() {
  //initialisation here
  var a;

  it("and so is a spec", function() {
    //then you run the code you want to test
    a = true;
    //and you test
    expect(a).toBe(true);
  });
});

因此,让我们把代码放在好的地方:

describe('get_user_input', () => {
  window.document.cookie = 'test_case=';
  var user_input = 'test';
  it('should update the cookie with users input if the input is NOT undefined', () => {

    if (typeof user_input !== "undefined") {
      var date = new Date;

      user_input = user_input.replace(/(\r\n|\n|\r)/gm, ' ');
      date.setDate(date.getDate() + 1);
      window.document.cookie = "user_input=" + user_input + ';path=/;expires=' + date.toGMTString();
    }

    expect(window.document.cookie).toEqual('test_case=test');
  });
});

编辑:即使这样也不会通过。 window.document.cookie包含路径和到期日期。

describe('get_user_input', () => {
      window.document.cookie = 'test_case=';
      var user_input = 'test';
      it('should update the cookie with users input if the input is NOT undefined', () => {

          if (typeof user_input !== "undefined") {
            var date = new Date;
            user_input = user_input.replace(/(\r\n|\n|\r)/gm, ' ');
            date.setDate(date.getDate() + 1);
            window.document.cookie = "user_input=" + user_input + ';path=/;expires=' + date.toGMTString();
          }

          expect(window.document.cookie).toEqual('test_case=test;path=/;expires=' + date.toGMTString() + ');
          });
      });