我正在尝试在 meteor 项目上运行 Jasmine 客户端集成测试。我正在使用 meteor 0.9.4
,以及用于 Jasmine 的 sanjo:jasmine
包。
我写了一个测试,如下所示:
describe("Template.dashboard.tasks", function() {
it("ela displays correct assessment", function() {
Session.set("selected_subject", "math");
Session.set('selected_grade', "1");
tasks = Template.dashboard.tasks();
expect(true).toBe(true);
});
});
在测试结束之前我得到一个错误:
Cannot read property 'tasks' of undefined
这意味着 Template.dashboard
不存在于此测试范围内。
Template.dashboard.tasks()
是一个完全有效的辅助函数,它位于 View 文件夹内的 js
文件中。常规 Jasmine
测试按预期工作,但一旦我尝试使用另一个文件中我自己的函数之一,它就不起作用。
我的问题是:我需要做些什么才能让 Jasmine
测试访问我的模板辅助函数吗?
最佳答案
在 Meteor 中,模板辅助函数的格式过去是这样的:
Template.dashboard.tasks = function () {
...
};
但这已被弃用,新格式是:
Template.dashboard.helpers({
tasks: function(){
...
}
});
在 Jasmine 中,使用之前的格式,您可以访问如下辅助函数:
Template.dashboard.tasks();
但是现在你必须像这样调用辅助函数:
Template.dashboard.__helpers[' tasks']();
Sanjo (meteor-jasmine repo 的原作者)建议使用这样的函数来更轻松地调用辅助函数(尤其是语法最终再次更改时):
function callHelper(template, helperName, context = {}, args = []) {
template.__helpers[` ${helperName}`].apply(context, args);
}
关于javascript - 在 Jasmine 中访问 Meteor 模板辅助函数以进行集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26616277/