业力茉莉花没有执行所有测试

时间:2015-07-31 14:14:26

标签: angularjs unit-testing jasmine karma-jasmine

所以我有6个业力茉莉花测试,在一个文件中我有3个,我测试我所有的工厂

工厂的测试文件如下

describe('Quiz Factories', function() {

    beforeEach(function() {

        //Ensure angular modules available
        beforeEach(module('geafApp'));

        beforeEach(inject(function (_counter_) {
            counter = _counter_;
        }));

        beforeEach(inject(function (_answer_sheet_) {
            answer_sheet = _answer_sheet_;
        }));

        beforeEach(inject(function (_questions_) {
            questions = _questions_;
        }));

    });

    it('it should return a number', function () {
        expect(counter).toBeNumber();
    });

    it('it should return an empty object', function () {
        expect(answer_sheet).toBeEmptyObject();
    });

    it('it should return an empty object', function () {
        expect(questions).toHaveObject(answers);
    });

});

在我的控制台日志中显示执行4个中的4个

  

PhantomJS 1.9.8(Mac OS X 0.0.0):执行4中6成功(0.004秒)   / 0.029秒)

因此出于某种原因,在第一次之后它就会出现这种情况。在工厂测试文件中,它跳过其他两个,即使没有失败,所有都包含在beforeEach中

3 个答案:

答案 0 :(得分:7)

除了接受的答案,这对于测试来说是一个更好的结构,我发现了可重现的情况:在测试中找到的嵌套beforeEach部分导致Karma停止运行任何进一步的Jasmine测试。您可以在问题中看到确实如此 - 注入的beforeEach位于外部beforeEach内。

作为合并的一部分,加载被测模块的beforeEach行之一已在稍后的beforeEach内无意中移动。这阻止了那次运行之后的所有测试。据报道,Karma正在运行x次y测试,其中x小于y,但测试运行成功且没有跳过。

因此,如果您遇到这种情况,请检查您的报告输出,以查看最后一次“成功”执行测试(我说成功引用了引号,因为它可能是导致问题的那个)并查看它是否没有嵌套beforeEach在里面。

答案 1 :(得分:3)

那么,让我们从这里开始吧。将您的文件更改为此以清除一些内容并查看它是否消失。您还需要在上一次测试中定义答案

describe('Quiz Factories', function() {

  var counter, answerSheet, questions;

  beforeEach( function(){
    module( 'geafApp' );

    inject( function( _counter_, _answer_sheet_, _questions_ ){
      counter = _counter_;
      answerSheet = _answer_sheet_;
      questions = _questions_;
    });
  });

  describe( 'when a question is asked', function(){
    it( 'should return a number', function(){
      expect( counter ).toBeNumber();
    });

    it( 'should return an empty object', function(){
      expect( answerSheet ).toBeEmptyObject();
    });

    it( 'should return an empty object', function(){
      expect( questions ).toHaveObject( answers ); // ??? answers is not defined!!!!
    });
  });
});

答案 2 :(得分:1)

也相关:

如果在测试击中任何expect之前抛出了异常,这也可能会发生-这不会导致测试被报告为失败-因为它从未运行过。它会 显示为减少的运行测试次数。在这种情况下,您可以检查控制台日志中的错误。