【问题标题】:jasmine-node - including helper茉莉花节点 - 包括助手
【发布时间】:2014-02-19 01:01:33
【问题描述】:

我正在尝试使用jasmine-node 测试我的Meteor 应用程序。我在帮助程序 (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();
        });
    });
});

任何帮助将不胜感激。

【问题讨论】:

  • 顺便说一句,这也可能表明您的要求配置存在问题。我假设您正在使用 --requireJsSetup 和 --runWithRequireJs 开关。可能需要仔细检查所有配置是否正确。

标签: node.js meteor jasmine jasmine-node


【解决方案1】:

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

注意:您可以作弊并改用helper,因为它是helpers 的子字符串...如果您将助手拆分到多个文件中会更有意义...单数与复数。

如果您从 specs/unit 执行规范,则创建一个名为 specs/unit/meteor-helper.js 的文件,jasmine_node 将自动为您获取它。如果您的规范是用原生 JavaScript 编写的,它将加载扩展名为 .js 的文件。如果你在命令行或通过你的 grunt 任务配置传递 --coffee switch(如果你有野心,你甚至可以使用 gulp),那么它将加载带有扩展名 js|coffee|litcoffee 的帮助程序。

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

specs/unit/meteor-helper.js

// file name must contain the word helper // x-helper is the convention I roll with module.exports = { key: 'value', Meteor: {} }

然后,jasmine_nodewrite each key 转为global namespace

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

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

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

更新:看来jasmine-node 只会在包含单词helpers 的情况下获取您的文件。将每个帮助文件命名为 x-helper.js|coffee|litcofee 应该可以解决问题。即meteor-helper.coffee

【讨论】:

  • 并记得将 --test-dir 设置为助手所在的文件夹。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 2014-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多