在Jasmine测试失败后执行代码

The*_*per 6 logging jasmine

只有在Jasmine测试失败的情况下才可以做某事吗?并列与afterEach()之后执行it()无论结果如何,我正在寻找一些方法来执行代码只有在it()一个失败的期望之后.

这个问题不是Angular特有的,但在我的场景中,我正在测试一个Angular服务,它使用输出调试消息$log.我不想让我的控制台混乱以获得成功的测试,但只显示失败测试的附加信息.

describe("MyService", function() {
    var MyService, $log;

    beforeEach(function() {
        inject(function (_MyService_, _$log_) {
            MyService = _MyService_;
            $log = _$log_;
        });
    });

    afterEach(function () {
        if (/* test failed */) {
            //console.log($log.debug.logs);
        }
    });

    it("should output debug logs when this fails", function() {
        //do something with MyService that would put a message in $log
        expect(false).toBe(true);
    });
});
Run Code Online (Sandbox Code Playgroud)

我正在运行Jasmine 2.2.0.

编辑:是一个非常简单的小提琴,显示Jasmine 1.3 jasmine.getEnv().currentSpec解决方案不再有效.

tlr*_*son 8

这里是一个黑客重新启用jasmine.getEnv().currentSpec在茉莉花2(排序的,result是不完整的spec对象,但包含id,description,fullName,failedExpectations,和passedExpectations):

jasmine.getEnv().addReporter({
    specStarted(result) {
        jasmine.getEnv().currentSpec = result;
    },
    specDone() {
        jasmine.getEnv().currentSpec = null;
    }
});
Run Code Online (Sandbox Code Playgroud)