我试图对请求正文进行断言,以确保正确的新测试卡作为订单的一部分通过。
it("User clicks confirm & pay button to complete order", () => {
cy.intercept("/api/checkout/payandcommit").as("placeOrder");
cy.placeOrderAndPay();
cy.wait("@placeOrder")
.its("response.statusCode")
.should("eq", 200)
.its("request.body")
.should("include", "cardNumber", 370000000000002);
});
Run Code Online (Sandbox Code Playgroud)
一切都很好,直到验证状态代码然后它就中断了。
这是抛出的错误:
Timed out retrying: cy.its() errored because the property: request does not exist on your subject.
cy.its() waited for the specified property request to exist, but it never did.
If you do not expect the property request to exist, then add an assertion such as:
cy.wrap({ foo: 'bar' }).its('quux').should('not.exist')
Run Code Online (Sandbox Code Playgroud)
如果我注释掉状态代码断言,则会引发此新错误:测试的对象必须是数组、映射、对象、集合、字符串或弱集,但给定对象。
任何帮助让这项工作正常进行将不胜感激!
小智 8
Chaining the assertions like this doesn't work, because the subject changes inside the chain
cy.wait("@placeOrder") // yields interception object
.its("response.statusCode") // yields number 200
.should("eq", 200) // yields it's input (number 200)
.its("request.body") // not testing the interception object here
.should("include", "cardNumber", 370000000000002);
Run Code Online (Sandbox Code Playgroud)
One way that works is to use a callback which gets the interception object
cy.wait('@placeOrder').then(interception => {
console.log(interception); // take a look at the properties
cy.wrap(interception.response.statusCode).should('eq', 404);
cy.wrap(interception.request.body)
.should("include", "cardNumber", 370000000000002) // not sure this should is correct
.should("have.property", "cardNumber", 370000000000002) // maybe this is better
})
Run Code Online (Sandbox Code Playgroud)
You may also be able to use chained commands if the subject is maintained, which means you have to tweak the should()
in the middle
cy.wait("@placeOrder")
.should('have.property', 'response.statusCode', 200)
.should('have.property', 'request.body.cardNumber', 370000000000002);
Run Code Online (Sandbox Code Playgroud)
Check out the logged interception object to make sure you have the correct properties and property value types (e.g is cardNumber a number or a string?).
归档时间: |
|
查看次数: |
5544 次 |
最近记录: |