gpt4 book ai didi

node.js - Jasmine Node - 包括助手

转载 作者:搜寻专家 更新时间:2023-10-31 22:59:37 24 4
gpt4 key购买 nike

我正在尝试测试我的 Meteor申请 jasmine-node .我在帮助程序 (spec_helper.js) 中删除了 Meteor 框架的一些方法:

  var Meteor = {
startup: function (newStartupFunction) {
Meteor.startup = newStartupFunction;
},
Collection: function (collectionName) {
Meteor.instantiationCounts[collectionName] = Meteor.instantiationCounts[collectionName] ?
Meteor.instantiationCounts[collectionName] + 1 : 1;
},
instantiationCounts: {}
};

此时我需要在 spec_helper.js 中运行代码(相当于包含其他语言的模块)。我尝试了以下方法,但没有成功:

require(['spec_helper'], function (helper) {
console.log(helper); // undefined
describe('Testing', function () {
it('should test Meteor', function () {
// that's what I want to call from my stubs...
// ...it's obviously undefined
Meteor.startup();
});
});
});

如有任何帮助,我们将不胜感激。

最佳答案

jasmine_node 将从您的规范目录中自动加载助手(任何包含单词 helpers 的文件)。

注意:您可以作弊并使用 helper 代替,因为它是 helpers 的子字符串...如果您将 helpers 分开会更有意义跨多个文件...单数与复数。

如果您从 specs/unit 执行规范,则创建一个名为 specs/unit/meteor-helper.js 的文件,以及 jasmine_node 将自动为您提供源代码。它将加载扩展名为 .js 的文件如果您的规范是用 vanilla JavaScript 编写的。如果你通过 --coffee switch在命令行上或通过你的 grunt 任务配置(如果你有野心,你甚至可以使用 gulp),然后它将加载扩展名为 js|coffee|litcoffee 的助手。 .

您应该从每个帮助文件中导出一个哈希,如下所示:

specs/unit/meteor-helper.js


//文件名必须包含单词 helper
//x-helper 是我使用的约定
模块.exports = {
核心值(value)',
meteor :{}
}

然后,jasmine_nodewrite each keyglobal namespace .

这将允许您从您的规范或任何被测系统(通常是您的 lib 文件夹中的代码)简单地键入 keyMeteor规范正在执行断言)。

此外,jasmine_node 还允许您通过 --nohelpers 开关抑制 helper 的加载(更多详细信息,请参阅 codeREADME)。

这是通过 Node 处理 Jasmine 助手的正确方法。您可能会遇到一些引用 jasmine.yml 文件的答案/示例;或者甚至 spec_helper.js。但请记住 this is for ruby land而不是 Node 。

更新:jasmine-node 似乎只有在包含单词helpers 时才会获取您的文件。将每个帮助程序文件命名为 x-helper.js|coffee|litcofee 应该可以解决问题。即 meteor-helper.coffee

关于node.js - Jasmine Node - 包括助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21868851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com