我该如何为我的Meteor方法编写单元测试?

3
我发现这有点复杂,如果我把meteor methods写在/lib文件夹里,那就更复杂了。我的目标是从服务器测试文件夹中测试我的方法(单元测试),但是存根this.userId以及在服务器端调试或显示日志并没有太大帮助。
我遇到了太多的问题,我正在使用 mochajs 和 velocity,有人能帮帮我吗?是否有人知道如何编写用于 meteor methods 的单元测试?

1
请参见此处讨论。也许callInContext是您需要的,但我不确定。 - David Weldon
非常感谢你,David。你提供的链接包含了太多有用的信息。 - Zilev av
1个回答

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


这段代码来自于上面提到的链接。

Meteor测试手册,这个Meteor.publishFunctions ['chapters'] .apply(thisContext); 只适用于发布函数吗?还是适用于Meteor方法? - Zilev av
这是一个很好的例子,Meteor.methodMap.serverMethod.call(thisContext),https://dev59.com/qojca4cB1Zd3GeqPvFQ- - Zilev av
链接已失效,在我的Meteor 1.2.1服务器端上,Meteor.publishFunctions未定义。 - Alexander Skiller

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接