【问题标题】:How can I configure the jasmine's random on gruntfile?如何在 gruntfile 上配置 jasmine 的随机数?
【发布时间】:2019-06-09 18:36:14
【问题描述】:

如何使用 grunt-contrib-jasmine 配置随机选项?我可以直接使用 jasmine 的命令行来完成,但是通过 grunt-cli 运行 jasmine 的任务我没有找到随机选项。然后命令行的输出总是显示规范的随机输出。

【问题讨论】:

    标签: javascript node.js gruntjs grunt-contrib-jasmine


    【解决方案1】:

    如果您使用 jasmine.d.ts 并且您的测试在 typescript 中,您还可以在 jasmine.d.ts 的 Env 接口中添加如下功能:

    interface Env {
        // some code
        // add function:
        configure(b: any): void;
    }
    

    然后在您的测试中,您可以编写如下内容:

    /// <reference path="../../../../typings/jasmine/jasmine.d.ts" />
    jasmine.getEnv().configure({ random: false });
    

    我测试了这种方法,最后我不必在每个描述函数中将随机选项设置为 false。我在引用路径之后添加了它,它适用于所有测试。

    编辑:您还可以将 jasmine 配置包含在 grunt-contrib-jasmine 任务的 options/helpers 部分中,作为单独的文件。比如:

    jasmine: {
        src: [some source files],
        options: {
          specs: [some spec files],
          helpers: 'helpers.js'
        }
    }
    

    【讨论】:

      【解决方案2】:

      我找到了我的问题的答案。至少我已经测试过并且它有效。 在每个 describe 声明的顶部,您可以配置 Suit Test 的随机选项。可以用以下语句:

      describe('My suite', function(){
         jasmine.getEnv().configure({random:false});
      
         // There are several tests here... 
      
         afterAll(function(){
            jasmine.getEnv().configure({random:true});
         });
      
         ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-19
        • 2013-03-31
        • 1970-01-01
        • 2023-03-31
        • 2020-03-13
        • 2022-01-17
        相关资源
        最近更新 更多