你應該使用像你這樣的fetch-mock
鏈接來測試新的fetch API調用。
構建失敗的事實對您來說絕對沒有意義。這只是讓開發人員知道何時向github作出持續發展。當你使用npm i --save-dev fetch-mock
時,它會下載(希望)github作者專門發佈的工作發佈版本,而不是失敗的版本。從我的項目的終極版
例咚行動:
import expect from 'expect';
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import fetchMock from 'fetch-mock';
import * as actions from './formActions';
const mockStore = configureStore([thunk]);
const store = mockStore({});
const body = {
email: '[email protected]',
};
describe('registerFormActions',() => {
let calledActions;
beforeEach(() => {
store.clearActions();
calledActions = store.getActions();
});
afterEach(() => {
fetchMock.reset().restore();
});
it('should post form',() => {
fetchMock.postOnce('/account/register', body);
store.dispatch(actions.submit()).then(() => {
expect(fetchMock.called('/account/register')).toBe(true);
});
});
});
export const submit = formData => dispatch =>
fetch('/account/register', {
method: 'post',
body: formData,
credentials: 'same-origin',
}).then((response) => {
if (response.ok) {
// Do something cool
}
});
嗯,好從別人已經用它,然後聽到。我會繼續向前推進。謝謝,害怕掉下兔子洞。 –
沒問題。下一次我會看看這個文檔,如果它看起來像你需要的那麼沒有理由不使用它,特別是對於不是生產代碼的單元測試。 –
fetch-mock也解決了我的問題。 :) – Ron