2015-04-02 54 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

相关问题