Javascript 在Jasmine中访问Meteor模板辅助函数进行集成测试
我试图在meteor项目上运行Jasmine客户端集成测试。我正在使用meteor 0.9.4和jasmine的sanjo:jasmine包 我写了一个测试,看起来像:Javascript 在Jasmine中访问Meteor模板辅助函数进行集成测试,javascript,unit-testing,templates,meteor,jasmine,Javascript,Unit Testing,Templates,Meteor,Jasmine,我试图在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"); Se
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()
是一个完全有效的帮助函数,它位于视图文件夹中的js
文件中。常规的Jasmine
测试按预期工作,但只要我尝试从另一个文件使用我自己的函数,它就不工作了
我的问题是:我是否需要做些什么来让Jasmine测试访问我的模板助手函数?服务器部分的测试从一开始就运行得很好,为了在前端部分运行测试,确实还有一件事要做。我会尽力帮你找到的
此外,考虑阅读或阅读Llama博士关于Jasmin /流星的博客文章中的著名和明确的文章:
流星,模板帮助函数的形式是这样的:
Template.dashboard.tasks = function () {
...
};
Template.dashboard.__helpers[' tasks']();
但这已经被弃用,新的格式是:
Template.dashboard.helpers({
tasks: function(){
...
}
});
在Jasmine中,使用前面的格式,您可以访问帮助器函数,如:
Template.dashboard.tasks();
但现在必须像这样调用帮助器函数:
Template.dashboard.tasks = function () {
...
};
Template.dashboard.__helpers[' tasks']();
(的原始作者)建议使用这样的函数来更容易地调用helper函数(特别是在语法再次更改的情况下):
Meteor 1.3对此问题的更新答案(抱歉,我使用摩卡咖啡,但它不会影响答案): 测试时不会急于设置Template.foo和Template.foo助手,因此需要导入
foo.html
,然后导入
以下是一个例子:
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { Foo } from '/collections/foo.js';
import { assert } from 'meteor/practicalmeteor:chai';
import './foo.html'; // now Template.foo is defined
import './foo.js'; // now Template.foo.__helpers[' bar'] is defined
describe('foo handlers', () => {
it("Should test bar", () => {
// don't forget the space, helper key is ' bar' not 'bar'
var bar = Template.foo.__helpers[' bar'].apply({foo:"bar"},3);
assert.Equal(bar,'bar');
});
});
当然,正如前面所说的,您肯定应该将奇怪的
Template.foo.\uu helpers['bar'].apply(context,args)
封装到一个漂亮、干净的helper中。我已经读完了那篇文章——我学到了很多,但我仍然无法让测试正常工作。你说还有一件事要做才能让客户端测试正常工作,那会是什么?