【发布时间】:2021-02-04 00:18:12
【问题描述】:
嗨,我在单元测试用例中断言状态时遇到问题,而它在 axios 调用中更新。 这是我的 axios 调用
import axios from 'axios'
getData = () => {
axios.get('/events/sept/week2/1097338')
.then(res =>{
if(res.status == 200){
let resposne = res.data.resposne
this.setState({weekData:response})
}
})
.catch(error => {console.log(error)})
}
这是我的测试用例
test("test axios for week data",() => {
const mock = new MockAdapter (axios)
mock.onGet('/events/sept/week2/1097338').reply(200,{response:['raspberry']})
const wrapper = shallow(<Component/>)
wrapper.instance().getData()
expect(wrapper.state('weekData')).toBe(['raspberry'])
})
当我调用时,组件中的状态 weekData 正在更新 wrapper.instance().getData(),我已经检查过了。它没有在包装器中更新,我的断言失败,如:预期:['raspberry'],收到:[]。如何更新包装器中的状态我尝试过 setTimeout 但没有用
【问题讨论】:
-
您使用github.com/ctimmerm/axios-mock-adapter 还是其他?应该提到这一点,因为问题是特定于您如何模拟 Axios。
-
是的,我正在使用link
标签: reactjs unit-testing axios jestjs enzyme