【问题标题】:'expect' was used when there was no current spec, this could be because an asynchronous test timed out in Jasmine 2.3.1'expect' 在没有当前规范时使用,这可能是因为 Jasmine 2.3.1 中的异步测试超时
【发布时间】:2015-12-26 06:36:06
【问题描述】:

我正在通过 gulp 运行 karma 测试用例,如下所示:

gulp.task('unit-test-karma', function () {
    return gulp.src(filePaths.libraryPaths.concat(filePaths.codePathsVerbose.concat(filePaths.testPaths).concat(filePaths.htmlPaths).concat(filePaths.jadePaths)))
        //.pipe(plumber({ errorHandler: notify.onError(function(error) { console.log(error.message); return "Karma Error"; }) }))
        .pipe(karma({
            configFile: './karma.conf.js',
            action: 'run', // watch
            singleRun: true,
            reporters: [ 'dots' ]
        }));
});

当我以run 的身份运行时,IE 11 会抛出以下错误。

IE 11.0.0 (Windows 10 0.0.0) ERROR
  'expect' was used when there was no current spec, this could be because an asynchronous test timed out
  at C:/BbCAT-WebDI/BbCAT-Web/BbCAT-Angular/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:938

但如果运行与watch 相同的操作,则所有测试用例在 chrome、IE 和 firefox 中都成功执行。

在阅读了一些帖子后,似乎 $http 服务调用存在一些问题,但无法找到问题的确切位置!

【问题讨论】:

  • 您是否在单元测试中使用$httpBackend 来存根您的$http 服务调用? docs.angularjs.org/api/ngMock/service/$httpBackend
  • 是的。这就是我想知道的原因,因为我正在测试的指令函数有 4 个 http 调用,并且都是用假响应模拟的,那么为什么它会抛出这个错误!
  • 您是否消除了其他测试导致错误抛出的可能性,即确保您怀疑的测试是唯一正在运行的测试?
  • 请给我们一些代码!
  • 你的代码一团糟。这不是测试用例的样子。请重构它并使用 //given//when//then 模式,也只用一个单元案例测试一个,否则它将是一个庞大、复杂且不可维护的测试案例。此外,您拥有 10 多个 $httpBackend 模拟也是不正常的。尝试清除它,然后我们可以帮助找出问题所在。

标签: angularjs jasmine gulp karma-runner


【解决方案1】:

这是一个非常现实的问题,我目前也遇到了。我认为这里有一个核心错误。我有很好的封装测试。它们很小(每个最多 3 行)

我有一个包含 2 个嵌套描述的主要描述部分 首先 describe 有 8 个 it() 函数 第二个有 3it() 函数。

describe("main", ()=>{
    describe("1st", ()=>{
        //here are 8 it() definitions
    })
    describe("2nd", ()=>{
        //here are 3 it() definitions
    })
})

现在,当我从任一描述中删除单个 it() 定义时,问题就消失了。或者,如果我添加第三个 describe(),问题就会消失。

这是 jasmine 中的一个问题 - 要么他们没有正确报告错误,要么出现了严重错误。或者它可能是通过同时运行多个测试来变得聪明的因果报应。无论哪种方式,这个问题都是真实的,它与混乱的代码无关。

也许它与正在测试的底层单元有关 - 我的函数是递归的(尽管我的测试用例没有深入研究)。

karma-jasmine@0.3.8

jasmine-core@2.4.1

业力@0.13.22

phantomjs-prebuilt@2.1.7

karma-phantomjs-launcher@1.0.0

【讨论】:

  • 此外,如果我不添加第 3 个描述,而是添加第 9 个 it() 定义,问题就会再次消失。 - 它肯定在某处比赛......
  • 请尝试更改为 chrome 启动器并重新运行。这将提供两件事。 1 phantomjs 给出了可怕的错误。 2.希望是phantomjs。因为 chrome 无头运行
  • 描述中存在超过 10 个测试的已知问题。较新版本的 jasmine 解决了这个问题
【解决方案2】:

您是否可以嵌套几个应该分开的测试,或者在同一个测试用例中解决多个异步调用?

我产生了同样的错误,但这是我自己造成的。我在一个 it() 中有两个异步测试。一旦任何一个承诺得到解决,测试就结束了。另一个 Promise 解决方案是孤立的。

考虑一下这些 sn-ps。假设被测函数在调用时正确响应。

注意:为了更清楚地说明问题,我省略了 then() 中的错误路径。

这个构造失败了。当任何一个 promise 返回并触发 done() 时,第二个现在会失败,并出现“没有当前规范时使用了'expect'...”错误。

describe( "delay", function(){
    var calculator = new Calculator();

    it( "delays execution - add and subtract", function(done){
        delay( 1000, calculator, 'add', [ 10, 5 ] )
            .then(function(result){
                expect(result).toEqual( 15 );
                done();  // <---- as soon as this runs, test is over
            });

        delay( 500, calculator, 'subtract', [ 9, 5 ] )
            .then(function(result){
                expect(result).toEqual( 4 );
                done(); // <---- as soon as this runs, test is over
            });
    });

} );

这是编写测试的正确方法。每个 Promise 都封装在自己的测试中。

describe( "delay", function(){
    var calculator = new Calculator();

    it( "delays execution - add", function(done){
        delay( 1000, calculator, 'add', [ 10, 5 ] )
            .then(function(result){
                expect(result).toEqual( 15 );
                done(); // <--- this is now the only resolution for  this test
            });
    });

    it( "delays execution - subtract", function(done){
        delay( 500, calculator, 'subtract', [ 9, 5 ] )
            .then(function(result){
                expect(result).toEqual( 4 );
                done(); // <--- this is now the only resolution for  this test
            });
    });

} );

由于我还没有足够的声誉来发表评论,我在此提出我的请求。 :-)

如果这是您的问题,您能否将此答案标记为正确?

【讨论】:

  • 我遇到了同样的错误,因为我正在测试异步代码,done() 可以解决问题
【解决方案3】:

这里有同样的问题,结果我用 setTimeout 进行了测试。清除了,一切都很好!

【讨论】:

    【解决方案4】:

    在 Jasmine 3.5 中也有此错误消息 - 它给我带来的影响超出了应有的范围,因为它正在谈论异步并且我在项目中有一些来自其他人的 jquery。

    这只是设置测试时的语法问题......我原来的

    it("should ...")
      expect(thing).toBe(whatever);
    })
    

    相对于工作...

    it("should ...", function(){
      expect(thing).toBe(whatever);
    })
    

    【讨论】:

      【解决方案5】:

      我遇到了这个错误,但这是因为我有一个 describe 函数,里面没有 it 函数。

      不正确

      describe('helpDocsDirective', function () {   
          expect(true).toBe(true);
      });
      

      正确

      describe('helpDocsDirective', function () {
          it("should return true", function () {
              expect(true).toBe(true);
          });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-09-23
        • 1970-01-01
        • 1970-01-01
        • 2013-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多