我使用 mocha / chai 來做一些測驗。如果使用相同的引數呼叫該函式,則應該拋出新的錯誤。
功能:
如果不存在具有相同 discord id 的用戶,則創建一個新用戶并將其保存到我的資料庫(mongoDB)中。使用from mongoosefetchUser(disc_id)搜索現有用戶。findOne()
async function createUser(disc_id, moodleToken) {
const profileData = await fetchUser(disc_id);
if (profileData) {
throw new Error("The account already exist in the database!");
}
const newUser = profileModel.create({
discord_id: disc_id,
moodle_token: moodleToken,
}, (err) => {
if (err) throw err;
console.log("Document created!")
})
return newUser;
};
取用戶
function fetchUser (disc_id) {
return profileModel.findOne({ discord_id: disc_id });
}
測驗
我使用 sinon 創建“測驗資料庫”。以下測驗通過就好了
describe("Create user", function () {
it("Should create a new user in database if the discord id is not in the database", async function () {
const discord_id = "2341243451";
const moodle_token = "fwefw89fwefHFEF0F90ls";
sinon.stub(profileSchema, "findOne").returns(null);
sinon.stub(profileSchema, "create").returns({
discord_id, moodle_token
});
const returnedUser = await createUser(discord_id, moodle_token);
expect(returnedUser.discord_id).to.equal(discord_id);
})
如果沒有現有用戶具有相同的不和諧 ID,這將測驗是否可以創建新用戶并將其保存到我的資料庫中。
如果我想測驗相反,創建一個新用戶,但當前用戶已經存在相同的 id,它應該拋出錯誤:“該帳戶已經存在于資料庫中!”
it("Should throw an error if there exists a user with that discord id", async () => {
const discord_id = "2341243451";
const moodle_token = "fwefw89fwefHFEF0F90ls";
const fakeObject = {
discord_id: discord_id,
moodle_token: moodle_token
};
sinon.stub(profileSchema, "findOne").returns(fakeObject);
sinon.stub(profileSchema, "create").returns({ discord_id, moodle_token })
我試圖跟隨但沒有成功:
try {
await createUser(discord_id, moodle_token);
} catch (err) {
console.log(err);
expect(err).to.equal("The account already exist in the database!");
}
expect(async function () { await createUser(discord_id, moodle_token); }).to.throw("The account already exist in the database!");
expect(() => { createUser(discord_id, moodle_token)}).to.throw("The account already exist in the database!");
rejects(async () => { await createUser(discord_id, moodle_token); })
我應該如何測驗這樣的功能?
uj5u.com熱心網友回復:
我找到了解決問題的方法,我createUser()對此進行了重組:
async function createUser(disc_id, moodleToken) {
const profileData = await fetchUser(disc_id);
if (profileData) {
throw TypeError("The account already exist in the database!");
}
const newUser = new profileModel({
discord_id: disc_id,
moodle_token: moodleToken,
})
await newUser.save();
return newUser;
}
如果它們是這樣的,則測驗通過:
it("Should throw an error if there exists a user with that discord id", async () => {
const discord_id = "2341243451";
const moodle_token = "fwefw89fwefHFEF0F90ls";
const fakeObject = {
discord_id: "2341243451",
moodle_token: "fwefw89fwefHFEF0F90ls"
};
sinon.stub(profileSchema, "findOne").returns(fakeObject);
await createUser(discord_id, moodle_token).catch((error) => {
expect(error.message).to.equal("The account already exist in the database!")
})
不知道這是否是正確的方法,但如果我改變sinon.stub回傳null測驗將不會通過。
所以我想這是一種處理它的方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/453596.html
