我试图简单地从柏树测试中的一个路由请求数据:
it("Then it works", () => {
cy.server()
cy.route({
method: 'GET',
url: '/users/1',
response: true
})
axios.defaults.headers.common = {
"X-Requested-With": "XMLHttpRequest"
}
axios.get("http://localhost:8080/users/1").then(response => {
console.log(response)
expect(response.body).to.equal(true)
}).catch(error => console.log(error))
})
我得到的全部信息都是“错误:状态代码404的请求失败”,因此该路由似乎无法用于axios。在我的cypress.json中,我将基本URL配置为:
{
"baseUrl": "http://localhost:8080"
}
从我的观点来看,这基本上是来自文档的例子,我不知道为什么它是错误的。我知道,那个柏树只能处理XMLHttpRequests,所以我为axios配置了它,并且我想模拟一个通常在我的SPA中发生的调用。
发布于 2019-05-06 21:41:27
您是在测试内部进行调用,而cy.route
则有助于拦截前端请求。更多:记住(从Cypress文档)
Cypress命令被排队并异步运行。
我的意思是:它是前端应用程序,必须调用axios.get
,而不是直接从测试中调用。
你可以:
cy.visit
一个具有全局定义的axios
的页面it("Then it works", () => {
cy.server()
cy.route({
method: 'GET',
url: '/users/1',
response: true
})
cy.visit("http://localhost:8000"); // replace it with your working app
cy.window().then(win => {
axios.defaults.headers.common = {
"X-Requested-With": "XMLHttpRequest"
}
axios.get("http://localhost:8080/users/1").then(response => {
console.log(response)
expect(response.body).to.equal(true)
}).catch(error => console.log(error))
})
})
一切都应该正常。如果没有,请与它共享一个GitHub回购,我会帮你的
https://stackoverflow.com/questions/56002159
复制相似问题