2014-10-28 63 views
3

我正在嘗試在流星項目上運行Jasmine客戶端集成測試。我爲Jasmine使用meteor 0.9.4sanjo:jasmine包。在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()是一個輔助函數,它完全工作,它在一個視圖文件夾中的js文件中。定期Jasmine測試按預期工作,但只要我嘗試從另一個文件中使用我自己的功能之一,它不起作用。

我的問題是:有什麼我需要做的,以Jasmine測試訪問我的模板幫助函數?

回答

6

在流星中的模板輔助函數像這樣被格式化:

Template.dashboard.tasks = function() { 
    ... 
}; 

但是已經被棄用,而新的格式是:

Template.dashboard.helpers({ 
    tasks: function(){ 
     ... 
    } 
}); 

在茉莉,與以前的格式,你可以一CCESS輔助功能,如:

Template.dashboard.tasks(); 

但現在你必須調用輔助函數是這樣的:

Template.dashboard.__helpers[' tasks'](); 

Sanjo(該meteor-jasmine repo的所有者)使用這樣的功能,使其更容易調用輔助函數建議(特別是如果語法結束再次變更):

function callHelper(template, helperName, context, args) { 
    context = context || {}; 
    args = args || []; 
    template.__helpers[' ' + helperName].apply(context, args); 
} 
0

服務器部件上的測試從開始就運行良好,並且爲了在前端部件上運行測試確實還有一件事要做。我會盡力找到你的。

此外,考慮讀取或再次讀取來自於大博士的博客涉及到茉莉/流星著名的和明確的文章:Bullet-proof Meteor applications with Velocity, Unit Testing, Integration Testing and Jasmine

+0

我讀過那篇文章 - 我學到了很多東西,但仍然無法通過測試。你說還有一件事要做,讓客戶端測試起作用,那會是什麼? – 2014-11-03 14:26:12

2

這個問題已更新的答案流星1.3(對不起,我ü SE摩卡,但它並不影響答案):

Template.foo和Template.foo助手不會急於建立測試時,所以你需要導入foo.html然後foo.js

下面是一個例子:

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.__helpers[' bar'].apply(context,args)成漂亮的,乾淨的幫手。

相關問題