我已經成功地在一個簡單的應用程序上測試了一個已解決的承諾,我一直在努力學習ES6,但一直在測試可能存在網絡錯誤的情況。如何測試抓取api中的網絡錯誤?
我寫這個測試用例:
import {Weather} from '../js/weather';
import chai from 'chai';
import sinon from 'sinon';
import * as chaiAsPromised from 'chai-as-promised';
chai.use(chaiAsPromised);
let assert = chai.assert;
let should = chai.should;
describe('weatherbot-1', function() {
beforeEach(function() {
sinon.stub(window, 'fetch');
let data = '{"cod": "400", "message": "Nothing to geocode"}';
let res = new window.Response(data, {
ok: false,
status: 400,
headers: {
'Content-type': 'application/json'
}
});
window.fetch.returns(Promise.reject(res));
});
afterEach(function() {
window.fetch.restore();
});
it('should ensure that promise is rejected', function() {
let weather = new Weather(0, 0);
return assert.isRejected(weather.getWeather(), 'Rejected');
});
});
問題的方法是:
getWeather() {
let headers = new Headers();
let init = {
method: 'GET',
headers: headers,
cache: 'default'
};
let url = 'http://localhost:8080/weather?lat=' + '' + '&lon=' + this.longitude;
return fetch(url, init).then(function (response) {
return response.json();
}).catch((error) => {
return error;
});
}
爲了測試的承諾,我使用chai-as-promised
& chai
與mocha
& sinon
存根的fetch
API 。
每當我運行測試我得到這個錯誤:
AssertionError: expected promise to be rejected with an error including 'Rejected' but it was fulfilled with { type: 'default',
url: '',
redirected: false,
status: 400,
ok: false,
statusText: 'OK',
headers:
{ append: [Function: append],
delete: [Function: delete],
get: [Function: get],
getAll: [Function: getAll],
has: [Function: has],
set: [Function: set],
keys: [Function: keys],
values: [Function: values],
entries: [Function: entries],
forEach: [Function: forEach] },
body: {},
bodyUsed: false,
clone: [Function: clone],
arrayBuffer: [Function: arrayBuffer],
blob: [Function: blob],
json: [Function: json],
text: [Function: text] }
從什麼樣子,在Promise.reject()
可能無法正常工作。有沒有其他方法可以測試網絡錯誤?
感謝您的回答,我刪除了'catch',但我得到一個新的錯誤:在Object.compatibleMessage(測試/ rejectedPromise.js無法讀取屬性未定義 \t「的indexOf」: '類型錯誤825 :254) \t at test/rejectedPromise.js:8036:1432 \t at' –
thedeliciousmuffin
這聽起來像是'assert.isRejected'中的一個錯誤。我期望它假定拒絕值是一個帶有'message'屬性的'Error'。如果您向拒絕中添加「訊息」,錯誤是否會消失?我知道這不會是一個代表'fetch'拒絕,但它會幫助診斷問題。 – joews
事實上,讀取你的代碼更多 - Promise不會拒絕'fetch'。 'fetch'返回一個已解析的Promise,用於非200錯誤代碼的有效HTTP響應。 – joews