.then 在承诺解决之前触发
.then triggering before promise resolved
我正在使用 mocha 和 supertest 在 Node 中编写集成测试,并且必须在数据库中创建用户。我的问题是有时用户会在测试完成后得到保存。
这是我的用户创建函数:
async function createUser() {
const userParam = {
username: "User",
password: "123456789",
firstName: "Max",
lastName: "Mustermann",
role: JSON.stringify(Role.User),
};
const user = new User(userParam);
user.hash = bcrypt.hashSync(userParam.password, 10);
await user.save();
console.log("User saved!");
}
这是测试:
it("respond with 403 forbidden, because a standard user cant delete other users", function (done) {
createUser().then(
login(userLoginData).then((responseBody) => {
request(app)
.delete("/users/0000000000006204aefc242c")
.set(
"Authorization",
`Bearer ${responseBody.body.accessToken}`,
)
.expect(
403,
{
Error: "Forbidden",
message:
"Forbidden for your rank, if its not your own account.",
},
done,
);
}),
);
});
这是我从 mocha 得到的输出:
DELETE /
Successes
Admin saved!
User saved!
✓ respond with 200 ok, because admins are allowed to delete all users (431ms)
User saved!
✓ respond with 200 ok, because he is allowed to delete himself (195ms)
Errors
1) respond with 403 forbidden, because a standard user cant delete other users
User saved!
前两个测试正在完成,因为在测试之前创建了所需的帐户,但最后一个测试(如上所示)失败了。
这是当前 github 分支中文件的 link:
https://github.com/Knniff/loginbackend/blob/2fa/test/tests.js
我很高兴收到所有反馈,如果您有更好的方法来进行这些准备工作,我很想听听。
您测试的第 2 行有问题。您不必等待 promise 解析并在 createUser()
函数之后立即调用 login()
函数。您应该将 login()
函数调用包装到箭头函数 () => login(userLoginData)
中。
您可以对测试进行以下更改:
it("respond with 403 forbidden, because a standard user cant delete other users", function (done) {
createUser()
.then(() => login(userLoginData))
.then((responseBody) => {
request(app)
.delete("/users/0000000000006204aefc242c")
.set(
"Authorization",
`Bearer ${responseBody.body.accessToken}`,
)
.expect(
403,
{
Error: "Forbidden",
message:
"Forbidden for your rank, if its not your own account.",
},
done,
);
});
});
编辑:
此外,我建议在您的测试中使用 async/await:
it("respond with 403 forbidden, because a standard user cant delete other users", async function () {
await createUser();
const responseBody = await login(userLoginData);
await request(app)
.delete("/users/0000000000006204aefc242c")
.set(
"Authorization",
`Bearer ${responseBody.body.accessToken}`,
)
.expect(
403,
{
Error: "Forbidden",
message: "Forbidden for your rank, if its not your own account."
}
);
});
我正在使用 mocha 和 supertest 在 Node 中编写集成测试,并且必须在数据库中创建用户。我的问题是有时用户会在测试完成后得到保存。 这是我的用户创建函数:
async function createUser() {
const userParam = {
username: "User",
password: "123456789",
firstName: "Max",
lastName: "Mustermann",
role: JSON.stringify(Role.User),
};
const user = new User(userParam);
user.hash = bcrypt.hashSync(userParam.password, 10);
await user.save();
console.log("User saved!");
}
这是测试:
it("respond with 403 forbidden, because a standard user cant delete other users", function (done) {
createUser().then(
login(userLoginData).then((responseBody) => {
request(app)
.delete("/users/0000000000006204aefc242c")
.set(
"Authorization",
`Bearer ${responseBody.body.accessToken}`,
)
.expect(
403,
{
Error: "Forbidden",
message:
"Forbidden for your rank, if its not your own account.",
},
done,
);
}),
);
});
这是我从 mocha 得到的输出:
DELETE /
Successes
Admin saved!
User saved!
✓ respond with 200 ok, because admins are allowed to delete all users (431ms)
User saved!
✓ respond with 200 ok, because he is allowed to delete himself (195ms)
Errors
1) respond with 403 forbidden, because a standard user cant delete other users
User saved!
前两个测试正在完成,因为在测试之前创建了所需的帐户,但最后一个测试(如上所示)失败了。
这是当前 github 分支中文件的 link: https://github.com/Knniff/loginbackend/blob/2fa/test/tests.js
我很高兴收到所有反馈,如果您有更好的方法来进行这些准备工作,我很想听听。
您测试的第 2 行有问题。您不必等待 promise 解析并在 createUser()
函数之后立即调用 login()
函数。您应该将 login()
函数调用包装到箭头函数 () => login(userLoginData)
中。
您可以对测试进行以下更改:
it("respond with 403 forbidden, because a standard user cant delete other users", function (done) {
createUser()
.then(() => login(userLoginData))
.then((responseBody) => {
request(app)
.delete("/users/0000000000006204aefc242c")
.set(
"Authorization",
`Bearer ${responseBody.body.accessToken}`,
)
.expect(
403,
{
Error: "Forbidden",
message:
"Forbidden for your rank, if its not your own account.",
},
done,
);
});
});
编辑:
此外,我建议在您的测试中使用 async/await:
it("respond with 403 forbidden, because a standard user cant delete other users", async function () {
await createUser();
const responseBody = await login(userLoginData);
await request(app)
.delete("/users/0000000000006204aefc242c")
.set(
"Authorization",
`Bearer ${responseBody.body.accessToken}`,
)
.expect(
403,
{
Error: "Forbidden",
message: "Forbidden for your rank, if its not your own account."
}
);
});