私は反応成分のような機能を持っています。成功したリクエストと失敗したリクエストをテストするにはどうすればよいですか?どのようにapiリクエストを持った反応関数をテストするのですか?
deleteQuestion(id) {
axios.delete('/api/questions/' + id)
.then(response => {
this.setState({message: "Deletion Successful!"});
}).catch(error => {
var errorMessage = 'Question not deleted: ' + error.response.data.message;
this.setState({error: errorMessage});
});
}
私はテストのためにこれを行うことを考えていましたが、これは明らかに機能しません。基本的には、最終機能のconsole.logとアサーションは実行されません。
import React from 'react';
import { mount, shallow } from 'enzyme';
import axios from 'axios';
import axios from 'axios';
import QuestionList from './QuestionList';
import sinon from 'sinon';
beforeEach(function() {
// import and pass your custom axios instance to this method
moxios.install()
})
afterEach(function() {
// import and pass your custom axios instance to this method
moxios.uninstall()
})
it('should modals and <table>',() => {
const wrapper = shallow(<QuestionList/>);
wrapper.instance().deleteQuestion()
moxios.wait(function() {
let request = moxios.requests.mostRecent()
request.respondWith({
status: 200,
response: [
{ id: 1, question: 'Fred', answer: 'Flintstone' },
{ id: 2, question: 'Wilma', answer: 'Flintstone' }
]
}).then(function() {
console.log('hello')
expect(wrapper.state().message).to.equal('Deletion Successful');
done()
})
})
});
私はまったく同じ問題を抱えています。あなたはそれを解決しましたか? – AngelGris
これは動作しましたか? –