2014-01-09 52 views
2

我現在正在使用這個庫來嘗試讓我的流星應用(https://github.com/EventedMind/iron-router#using-a-layout-with-yields)服務器端的Handlebars工作。我怎樣才能讓茉莉花模擬數組值?

我已經得到它的工作,但我現在想修復我的單元測試。我在Jasmine有點初學,所以希望這個問題不是太愚蠢。告訴我,如果我完全在錯誤的軌道上。

目前我試圖在茉莉花單元測試中嘲笑這條線。

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

我知道如何模擬方法,但我不知道如何模擬數組值。

我試過這樣做。

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

但它給了我這個錯誤。

templates() method does not exist 

我該如何模擬[]並讓它返回一些東西?

回答

2

對添加間諜方式的小修改將解決問題。必須對該對象上的對象和函數/值進行間諜註冊。將註冊修改爲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); 
}); 
});