2015-04-02 83 views
3

我發現它有點複雜,如果我在文件夾中編寫我的meteor methods文件夾,我想要從服務器測試文件夾測試我的方法(單元測試),但存根this.userId並且還在服務器端調試或顯示日誌沒有太大的幫助。我該如何寫我的流星方法的單元測試?

我的問題太多了,我用速度使用mochajs,有人會幫助我嗎?有人知道我該如何將這些單位寫入流星方法?

+1

見的討論[這裏](https://forums.meteor.com/t/testing-methods-which-use-this-userid/)。這可能是'callInContext'是你需要的,但我不確定。 – 2015-04-02 21:54:49

+0

非常感謝大衛,在你提供給我的鏈接中有太多有用的信息 – 2015-04-22 22:23:22

回答

2

Mocha不支持單元測試,目前只有Jasmine。這是你如何在Jasmine爲服務器編寫單元測試並使用userId的例子。

it("should return premium content to logged in users", function() { 

// SETUP 
var thisContext = { 
    userId : true 
}; 

var expectedCursor = 'chapter_cursor1'; 
var _query = true, _modifiers = true; 
Chapters.find = function(query, modifiers) { 
    _query = query; 
    _modifiers = modifiers; 
    return expectedCursor; 
}; 

// EXECUTE 
var actualCursor = Meteor.publishFunctions['chapters'].apply(thisContext); 

// VERIFY 
expect(actualCursor).toBe(expectedCursor); 
expect(_query).toBe(undefined); 
expect(_modifiers).toBe(undefined); 

});

從這裏拍攝:https://github.com/xolvio/Letterpress/blob/master/tests/jasmine/server/unit/chaptersSpec.js#L3

+0

流星測試手冊,this Meteor.publishFunctions ['chapters']。apply(thisContext);僅適用於發佈功能?和流星的方法? – 2015-04-06 18:41:02

+0

這裏是一個很好的例子,Meteor.methodMap.serverMethod.call(thisContext),http://stackoverflow.com/questions/28796568/meteor-jasmine-velocity-how-to-test-a-server-method-requiring-logged -in-us – 2015-04-07 20:35:28

+0

鏈接已死,Meteor.publishFunctions在我的流星1.2.1服務器端未定義 – 2016-07-26 15:35:50

相關問題