如何让jasmine模拟数组的值?

时间:2014-01-09 12:28:50

标签: unit-testing meteor jasmine

我现在正在使用这个库来尝试让Handlebars在我的meteor app(https://github.com/EventedMind/iron-router#using-a-layout-with-yields)的服务器端工作。

我已经开始工作,但我现在想要修复我的单元测试。我是Jasmine的初学者,所以希望这个问题不是太愚蠢。告诉我,如果我完全走错了路。

目前我正试图在茉莉花单元测试中模仿这一行。

Handlebars.templates['ResponseToSubscribers']({dateSent: new Date()})

我知道如何模拟方法,但我不确定如何模拟数组值。

我试过这样做。

spyOn(Handlebars, 'templates').andReturn({"ResponseToSubscribers": (obj) -> "html"})

但它给了我这个错误。

templates() method does not exist

我如何模拟[]并让它返回一些东西?

1 个答案:

答案 0 :(得分:3)

对添加间谍的方式进行小幅修正可以解决问题。必须对该对象上的对象和函数/值进行间谍注册。将注册修改为spyOn(Handlebars.templates, 'ResponseToSubscribers')将解决您的问题。

示例代码:

describe("Test Array", function() {
it("checks the actual value", function() {
    var t1 = Handlebars.templates['ResponseToSubscribers']('dummy');
    expect(t1).toEqual(1);
});

it("checks handle bar value", function() {
    spyOn(Handlebars.templates, 'ResponseToSubscribers').and.returnValue(2);
    var t = Handlebars.templates['ResponseToSubscribers']('dummy');
    expect(t).toEqual(2);
});
});