gpt4 book ai didi

logging - Jasmine 测试失败后执行代码

转载 作者:行者123 更新时间:2023-12-04 13:31:41 24 4
gpt4 key购买 nike

只有在 Jasmine 测试失败时才能做些什么吗?与 afterEach() 并列在 it() 之后执行无论结果如何,我都在寻找某种方法来仅在 it() 之后执行代码有一个失败的期望。

这个问题不是特定于 Angular 的,但在我的场景中,我正在测试一个使用 $log 输出调试消息的 Angular 服务。 .我不想为成功的测试弄乱我的控制台,而只想显示失败测试的附加信息。

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);
});
});

我正在运行 Jasmine 2.2.0。

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

最佳答案

这是重新启用 jasmine.getEnv().currentSpec 的技巧在 Jasmine 2 中(有点, result 不是完整的 spec 对象,但包含 iddescriptionfullNamefailedExpectations 和 |7:104| )

jasmine.getEnv().addReporter({
specStarted(result) {
jasmine.getEnv().currentSpec = result;
},
specDone() {
jasmine.getEnv().currentSpec = null;
}
});

关于logging - Jasmine 测试失败后执行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31390105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com