jasmine2:如何处理 beforeAll 失败
jasmine2: how to handle failing beforeAll
我的 jasmine2/protractor 测试是这样的
var testUserId = null;
describe("user test", function() {
beforeAll(function(done) {
createTestUser().
.then(function(userId){testUserId = userId})
.then(done)
.catch(done.fail);
it("should do stuff with the test user", function(done) {
// bla bla
});
afterAll(function(done) {
deleteTestUser(testUserId).
.then(done)
.catch(done.fail);
});
})
createTestUser 和 deleteTestUser return 承诺。如果出现问题,他们会拒绝并显示错误消息。现在可能的问题是,即使在 beforeAll 中发生错误,测试也会开始。我得到
Failures:
1) should do stuff with the test user
Message: [my error message from beforeAll]
如果有很多测试,它会尝试执行所有测试并失败,并显示非常相同的错误消息。如果 beforeAll 函数失败,是否可以阻止它执行测试?
谢谢!
("jasmine-core": "2.8.0", "protractor": "5.2.1")
编辑:
这不是我想要的,但至少我找到了一个解决方案来保持错误消息的数量接近,如下所示:
var testUserId = null;
describe("user test", function() {
beforeAll(function(done) {
createTestUser().
.then(function(userId){testUserId = userId})
.then(done)
.catch(done.fail("test user could not be created"));
it("should do stuff with the test user", function(done) {
if (testUserId) {
// bla bla
} else {
done();
});
这样我至少得到了正确的错误信息("test user could not be created"),而不是“// bla bla”中未满足的期望(自然我仍然得到每个 it,但无论如何)。我还将函数包装在函数工厂中,这样我就不必每次都编写 if 条件。
据我了解,jasmine
does not support that尽管受到关注:
Skipping out in the middle of the spec run (this issue) is a bit more
complicated, because depending on the type of error, Jasmine probably
still needs to run any afterEach (or afterAll depending) to cleanup
state for the next spec. This would then require the QueueRunner to
know which of the functions it is given are setup and teardown and not
just have a list of functions to call.
一种选择是使用 "fail fast" 选项,这可以通过使用第三方库之一来完成,例如 jasmine-fail-fast
or protractor-jasmine2-fail-whale
.
虽然还有其他一些解决方法,例如手动检查 it()
函数中是否存在先前的故障:
- Jasmine/Protractor: stop test on failure in beforeEach
我的 jasmine2/protractor 测试是这样的
var testUserId = null;
describe("user test", function() {
beforeAll(function(done) {
createTestUser().
.then(function(userId){testUserId = userId})
.then(done)
.catch(done.fail);
it("should do stuff with the test user", function(done) {
// bla bla
});
afterAll(function(done) {
deleteTestUser(testUserId).
.then(done)
.catch(done.fail);
});
})
createTestUser 和 deleteTestUser return 承诺。如果出现问题,他们会拒绝并显示错误消息。现在可能的问题是,即使在 beforeAll 中发生错误,测试也会开始。我得到
Failures:
1) should do stuff with the test user
Message: [my error message from beforeAll]
如果有很多测试,它会尝试执行所有测试并失败,并显示非常相同的错误消息。如果 beforeAll 函数失败,是否可以阻止它执行测试?
谢谢!
("jasmine-core": "2.8.0", "protractor": "5.2.1")
编辑:
这不是我想要的,但至少我找到了一个解决方案来保持错误消息的数量接近,如下所示:
var testUserId = null;
describe("user test", function() {
beforeAll(function(done) {
createTestUser().
.then(function(userId){testUserId = userId})
.then(done)
.catch(done.fail("test user could not be created"));
it("should do stuff with the test user", function(done) {
if (testUserId) {
// bla bla
} else {
done();
});
这样我至少得到了正确的错误信息("test user could not be created"),而不是“// bla bla”中未满足的期望(自然我仍然得到每个 it,但无论如何)。我还将函数包装在函数工厂中,这样我就不必每次都编写 if 条件。
据我了解,jasmine
does not support that尽管受到关注:
Skipping out in the middle of the spec run (this issue) is a bit more complicated, because depending on the type of error, Jasmine probably still needs to run any afterEach (or afterAll depending) to cleanup state for the next spec. This would then require the QueueRunner to know which of the functions it is given are setup and teardown and not just have a list of functions to call.
一种选择是使用 "fail fast" 选项,这可以通过使用第三方库之一来完成,例如 jasmine-fail-fast
or protractor-jasmine2-fail-whale
.
虽然还有其他一些解决方法,例如手动检查 it()
函数中是否存在先前的故障:
- Jasmine/Protractor: stop test on failure in beforeEach