0

我试图检查对 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!


  
4

1 回答 1

1

我认为 chai 使用.property而 cypress 使用.attr. 您可以使用以下链,因为 a.should()命令返回它断言的对象,而不是链的原始元素

cy
    // .wait(14000)
    .get("@token")
    .its("response")
    .should("have.property", "body")
    .should('have.attr', "access_token")
于 2021-06-17T11:58:00.820 回答