【问题标题】:How do I test if a function is called in Meteor.startup using jasmine?如何测试是否使用 jasmine 在 Meteor.startup 中调用了函数?
【发布时间】:2015-11-12 20:23:31
【问题描述】:

我在名为 server.js 的文件中有以下代码:

Meteor.startup(function(){
    setupMail();
});

我想创建一个单元测试以确保在Meteor.startup 中调用setupMail。我怎么做?这就是我在 serverSpec.js 中的内容:

describe("Meteor startup", function(){
    it("should call setupMail", function(){
        spyOn(global, "setupMail").and.callThrough();
        expect(setupMail).toHaveBeenCalled();
    });
});

测试正在运行,但没有通过。我认为这是因为Meteor.startup 被存根。有什么方法可以测试一下回调中传递了什么?

【问题讨论】:

  • spyOn 不允许调用存根方法中的方法。所以任何启动调用都不会被调用。
  • 无赖。感谢您的信息。

标签: node.js meteor jasmine meteor-velocity


【解决方案1】:

请参考此链接:

http://xolv.io/blog-posts/2013/04/unit-testing-with-meteor

我喜欢这个

Meteor.startup(initServer);

而不是

Meteor.startup(函数(){ 初始化服务器 });

其中 initServer 仅包含 Meteor.publish 调用,并且没有遵循上面链接中给出的存根部分

在测试中:

describe('服务器启动', function(){

'use strict';

it('should publish Notes', function(){
    spyOn(Meteor, 'publish');
    Meteor.startup(initServer);
    expect(Meteor.publish).toHaveBeenCalledWith('notes', findNotes);
});});

希望对你有帮助

【讨论】:

  • 欢迎来到 StackOverflow。提供外部链接时,请说明其中包含哪些信息。
猜你喜欢
  • 2018-05-06
  • 2021-03-09
  • 2018-09-15
  • 1970-01-01
  • 2018-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-24
相关资源
最近更新 更多