流星中的单元测试找不到他们需要测试的代码

Unit tests in meteor can't find the code they need to test

我正在为 Meteor 中的单元测试而苦苦挣扎。我想使用 velocity, jasmine 包,但我一定做错了什么。测试似乎不起作用,因为测试找不到要测试的代码。测试项目在 github.

上可用

我要测试的代码在这里: https://github.com/robvanpamel/coderepository/blob/master/meteor/sandwich-app/server/Services/SandwichService.js

单元测试在这里: https://github.com/robvanpamel/coderepository/blob/master/meteor/sandwich-app/tests/jasmine/server/integration/spec/SandwichServiceSpec.js 当我在单元测试中取消注释创建的 SandwichService 时,测试有效,这是正常的。

我没有在 meteor 的其他地方做过任何配置,我认为这是问题所在。您必须在指定源代码的地方放置一个 package.js 文件吗? Jasmine 如何知道在哪里可以找到我要测试的 SandwichService?这也是我得到的错误。 "ReferenceError: SandwichesService is not defined "

编辑

我能够解决它并更新了 GitHub 中的代码存储库。关键是不要使用 Javascript 原型。所以以下将不起作用

function SandwichesService(){};

SandwichesService.prototype.listSandwiches = function() {
  // do stuff here
}

而下面的代码确实有效

SandwichService = {
  listSandwiches: function(){
      // do stuff here
  }
};

我不太明白为什么?有人可以告诉我吗?

在此先致以诚挚的问候和感谢! 罗布

这是一个 Meteor 问题,不是测试问题:)

您需要将 SandwichesService 放在全局命名空间中。您的第二个块默认执行此操作。

尝试:

SandwichService = new SandwichesService()

(不要使用变量)