![](/img/trans.png)
[英]Accessing a Meteor Template Helper Function in Jasmine for Integration Testing
[英]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.