【发布时间】:2021-09-02 04:31:25
【问题描述】:
我正在尝试检查对 auth0 的请求的响应正文是否返回包含属性 access_toke 的对象。 这是我的代码:
When("I attemt to login with currect user credentials", () => {
cy.intercept("https://punct-development.eu.auth0.com/oauth/token").as(
"token"
);
cy.loginWith({ email: email, password: password });
cy.wait("@token");
});
Then("Im succesfully logged in", () => {
cy
// .wait(14000)
.get("@token")
.its("response")
.should("have.property", "body")
.then((body) => {
expect(body).to.have.attr("access_token");
});
这是失败的一步,你可以看到我得到了响应体-
expected { Object (access_token, id_token, ...) } to have attribute access_token
但是当试图验证它有一个属性 access_token 时,我得到了以下错误()-
The chai-jQuery assertion you used was:
> attr
The invalid subject you asserted on was:
> Object{5}
To use chai-jQuery assertions your subject must be valid.
This can sometimes happen if a previous assertion changed the subject.
cypress/integration/Login/login.ts:29:28
27 | .should("have.property", "body")
28 | .then((body) => {
> 29 | expect(body).to.have.attr("access_token");
| ^
30 | });
31 | });
32 | ```
[test run screenshot][1]
[1]: https://i.stack.imgur.com/F5fRC.png
any help will be much appreciated!
【问题讨论】:
-
如果您的代码是正确的,您可以尝试将断言替换为 `cy.get(body).should('have,attr', "access_token")
-
@RosenMihaylov 我将其更改为 -
Then("Im succesfully logged in", () => { cy // .wait(14000) .get("@token") .its("response") .should("have.property", "body"); cy.get(body).should("have,attr", "access_token"); });,现在我的代码出现以下错误 - 找不到名称“body”。正确的语法是什么? -
它应该被链接为
.then,就像您在代码中所做的那样 -
cy // .wait(14000) .get("@token") .its("response") .should("have.property", "body")).should('have ,attr', "access_token")
-
@RosenMihaylov 我搞定了!我没有按照你的建议替换断言,只是替换了
expect(body).to.have.property("access_token");expect(body).to.have.attr("access_token");你认为这是一个合适的解决方案吗?
标签: cypress intercept cypress-cucumber-preprocessor