【问题标题】:How to write unit tests for a grunt plugin that doesn't write files?如何为不写文件的 grunt 插件编写单元测试?
【发布时间】:2014-02-18 10:29:05
【问题描述】:

我正在创建一个 grunt 插件,它负责在文件中查找 URL,并将这些 URL 添加到 grunt 文件中的变量中

grunt.config.set('urls', urls);

Grunt 插件的所有单元测试示例都假定插件将写入一个文件,然后将该输出与具有“预期”结果的测试文件进行比较。

有什么方法可以测试我的 Grunt 插件是否设置了配置变量?

【问题讨论】:

    标签: javascript unit-testing plugins gruntjs grunt-plugins


    【解决方案1】:

    这样的?

    urlConfigTest: function(test) {
        test.expect(1);
        grunt.config.set('urls','http://foo.example.com');
        test.equal(grunt.config.get('urls'), 'http://foo.example.com', 'Should set foo to the grunt configuration.');
        test.done();
    }
    

    【讨论】:

    • 完美运行。我没有意识到脚手架测试文件中对 grunt 的引用实际上保持了当前状态。
    【解决方案2】:

    grunt-plugins 的 Yo 生成器 (*) 基本上是这样设置测试:

    1. 设置您的 Gruntconfig.js 以使用特定的 testconfig 运行您的插件
    2. 然后通过创建测试任务来配置测试运行:grunt.registerTask('test', ['myPlugin', 'nodeunit']);。这意味着您的插件应该已经设置了您想要测试的配置值。
    3. test.equal(grunt.config.get('urls'), 'http://foo.example.com', 'Should ...');一样运行测试

    Yo 生成器由 Addy Osmani 和 Stephen Sawchuck 维护,可以算作可靠来源 ;-)

    (*)npm install -g generator-gruntplugin

    【讨论】:

      猜你喜欢
      • 2017-06-06
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      相关资源
      最近更新 更多