[英]Vuejs2 test http request on a form click
In my component i have 在我的组件中
methods:{
ContactUs(){
this.$http.post("/api/contact-us").then((res)=>{
///do new stuff
},(err)=>{
//do new stuff
})
},
}
Now i want to test that the method works 现在我要测试该方法是否有效
so in my test i have 所以在我的测试中
const wrapper = mount(ContactForm);
it("Contact us method should return a 200 response ", () => {
wrapper.vm.ContactUs().then((res) => {
expect(res.data).toEqual(res);
})
//await flushPromises();
});
But now the test fails and error points to this.$http.post... 但是现在测试失败,错误指向this。$ http.post ...
How do i test the above function 我如何测试以上功能
ContactUs needs to return a promise if you want to chain a .then() to it. 如果要将.then()链接到它,ContactUs需要返回一个承诺。 this.$http.post already returns a promise, just return its result from ContactUs and your test can chain to it. this。$ http.post已经返回了一个承诺,只需从ContactUs返回其结果,您的测试就可以链接到它。
ContactUs(){
return this.$http.post("/api/contact-us").then((res)=>{
///do new stuff
},(err)=>{
//do new stuff
})
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.