【发布时间】:2018-05-11 18:38:06
【问题描述】:
我的 jasmine2/量角器测试看起来像这样
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 返回承诺。如果出现问题,他们会拒绝并显示错误消息。现在可能的问题是,即使在 beforeAll 中发生错误,测试也会开始。我得到了
Failures:
1) should do stuff with the test user
Message: [my error message from beforeAll]
如果有许多测试,它会尝试执行所有测试并失败并显示非常相似的错误消息。如果 beforeAll 函数失败,是否可以阻止它执行测试?
谢谢!
("jasmine-core": "2.8.0", "量角器": "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();
});
这样我至少只得到正确的错误消息(“无法创建测试用户”),而不是“// bla bla”中未满足的期望(当然,我仍然会为每个 它,但无论如何)。我还将函数包装在函数工厂中,这样我就不必每次都编写 if 条件。
【问题讨论】:
-
你为什么不嘲笑
createTestUser创建的依赖关系?测试不应该因为依赖失败而失败。隔离自己! -
没错。让我们说这意味着对一个不是我做的架构进行太大的改变。在 realz 中,不仅创建了一个 testuser,还创建了许多其他的东西,而且使用了一个带有数据库和东西的真实后端,这可能会出错。是的,是的,我知道..
-
这听起来像是一个 x-y 问题。您可以对测试中的项目做任何事情。
标签: javascript jasmine protractor e2e-testing