簡體   English   中英

使用Jasmine測試Meteor輔助函數時出錯

[英]Error in testing a Meteor helper function with Jasmine

我在MeteorJS中有一個輔助函數,如下所示:

Template.cars.helpers({
    models : function() {
        var currentUserId = Meteor.userId();
        return cars.find({userId: currentUserId});
    }
});

我正在嘗試編寫一個Jasmine測試,該測試將檢查在調用models助手時是否一次調用Meteor.userId()函數。 我模擬了Meteor.userId()函數,下面給出了我的代碼:

describe("test cars collection", function() {

    beforeEach(function() {
        var Meteor = {
            userId: function() {
                return 1;
            }
        };        
    });

    it("userId should be called once", function() {
        Template.cars.__helpers[' models']();
        spyOn(Meteor, 'userId').and.callThrough();
        expect(Meteor.userId.calls.count()).toBe(1);
    });
}); 

但是,結果顯示Expected 0 to be 1.我是Jasmine的新手,並不真正知道如何在調用models助手時對Meteor.userId()函數進行正確的調用。 我認為我從事間諜活動的方式是錯誤的,但我無法弄清楚。 有人可以幫忙嗎?

茉莉花文檔

.calls.count() :返回間諜被調用的次數

但是在調用函數之前,您需要設置間諜,因此可以檢查函數是否僅被調用過一次,如下所示:

it("userId should be called once", function() {
    spyOn(Meteor, 'userId').and.callThrough(); // <-- spy on the function call first
    Template.cars.__helpers[' models'](); // <-- execute the code which calls `Meteor.userId()`
    expect(Meteor.userId.calls.count()).toBe(1); // <-- Now this should work
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM