【问题标题】:jasmine javascript basic function test is failing茉莉花javascript基本功能测试失败
【发布时间】:2017-01-03 09:07:51
【问题描述】:

您好,我正在尝试开始一个简单的茉莉花测试(使用打字稿),这似乎不起作用。我测试的是如果变量不为空,是否在内部函数的 else 部分调用函数。

我的功能:

let getAndSet = () => {
    if (main.getApiAcessToken() === "") {
        main.addIframe();
        main.getGraphToken();
    } else {
        getCountry();
    }
};

这是测试:

it("should call Function get country if token that was passed not empty", () => {
    let main2 = main;
    main2.getApiAcessToken = jasmine.createSpy("getApiAcessToken spy").and.returnValue("not empty");
    let getCountry2 = jasmine.createSpy("getCountry spy");
    getAndSet();
    expect(getCountry2).toHaveBeenCalled();
});

我似乎不明白可能是什么问题,我尝试更改为正常的 js 函数或 this。主要功能它们似乎都不起作用似乎我缺少茉莉花中非常基本的东西。

【问题讨论】:

  • 我可以看到你设置了测试。但是在测试中你在哪里调用 getAndSet 方法呢? (不是说你的设置是正确的,顺便说一句)
  • 谢谢我刚刚注意到,并修复了它,但仍然是同样的问题。还要更新代码以反映这一点。

标签: javascript unit-testing typescript jasmine automated-tests


【解决方案1】:

你的间谍的名字是错误的。您的间谍称为getCountry2,而在代码中该方法称为getCountry

getCountry = jasmine.createSpy("getCountry spy");
getAndSet();
expect(getCountry).toHaveBeenCalled();

这是一个工作示例:http://jsfiddle.net/bBL9P/506/

【讨论】:

  • 解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-30
  • 1970-01-01
相关资源
最近更新 更多