0
我有這個函數,它將電子郵件和訪問密鑰作爲參數,並對mailboxlayer api進行網絡調用,並返回帶有驗證結果的承諾。在使用Jest測試JavaScript異步代碼時遇到問題JS
function validateEmail(email, accessKey) {
return new Promise(function (fulfill, reject) {
request.get("http://www.apilayer.net/api/check?access_key=" +
accessKey + "&email=" + email + "&smtp=1&format=1")
.end(function (err, res) {
if (err) {
reject(err);
}
else {
fulfill(res.body);
}
});
});
}
我想測試這一點,所以我創建了一個下面的測試
describe('EmailValidator', function() {
it("Should take an email,accessKeys as parameters and return a Promise",
function() {
const data = {
email: "[email protected]",
did_you_mean: "",
user: "somemail",
domain: "gmail.com",
format_valid: true,
mx_found: true,
smtp_check: true,
catch_all: null,
role: false,
disposable: false,
free: true,
score: 0.8
};
obj.validateEmail("[email protected]","80867a1cafc7017cd9a9b510c01d1ba7")
.then(value => {
expect(data).toEqual(value);
})
});
});
但隨後獲取失敗的結果,在數據對象中(我用來比較網絡調用的結果),我只是將用戶值從用戶:「somemail」改爲用戶:「anothermail」。 所以在這裏,我期待測試失敗,因爲我比較的對象與我從網絡獲得的對象不同。但結果並不如預期,但測試通過一些建議。結果如下圖所示
我想知道這裏發生了什麼,我該如何實現這個功能,就像我期望的那樣,即讓這個測試失敗。
你太棒了。謝謝。有效 !!! –