2017-03-15 1 views
1

私は反応成分のような機能を持っています。成功したリクエストと失敗したリクエストをテストするにはどうすればよいですか?どのように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() 
     }) 
    }) 
}); 
+0

私はまったく同じ問題を抱えています。あなたはそれを解決しましたか? – AngelGris

+0

これは動作しましたか? –

答えて

0

モックhttpサーバーライブラリを使用できます。たとえば、https://www.npmjs.com/package/mock-http-server

+0

答えをより詳細に更新しましたが、主な問題は要求がwrapper.instance()。deleteQuestion()呼び出しで送信されていないことですが、依然として動作していません。 – dpst

関連する問題