【问题标题】:Understanding then() in Cypress了解赛普拉斯中的 then()
【发布时间】:2020-09-25 15:57:23
【问题描述】:

我正在阅读 Cypress 中的文档,我想我知道 then() 的作用。它像 Promise 一样工作,一个 Promise 返回另一个 Promise,但是使用 then(),我们返回一个新的主题。

如果我们看下面的代码示例,我们使用 then() 是因为我们正在返回一个新变量,在本例中称为 target。

我理解正确吗?如果没有,有人可以纠正我吗?

  it.only('Marks an incomplete item complete', () => {
         //we'll need a route to stub the api call that updates our item
         cy.fixture('todos')
         .then(todos => {
             //target is a single todo, taken from the head of the array. We can use this to define our route
             const target = Cypress._.head(todos)
             cy.route(
                 "PUT",
                 `api/todos/${target.id}`,
                 //Here we are mergin original item with an object literal 
                 Cypress._.merge(target, {isComplete: true})
             )
         })

【问题讨论】:

    标签: testing cypress


    【解决方案1】:

    .then 用于接收来自cy.fixture('todos') 的结果。变量target 在这段代码中并不重要。

    在您的代码示例中,从cy.fixture 返回的变量被命名为todos - 代码的间距可能会让您失望? .then 调用附加到cy.fixture() 调用

    // These 2 code blocks are the same - just different spacing
    cy.fixture('todos')
    .then(todos => {});
    
    cy.fixture('todos').then(todos => {});
    

    https://docs.cypress.io/api/commands/fixture.html#Usage

    cy.fixture('logo.png').then((logo) => {
      // load data from logo.png
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-26
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 2021-08-08
      • 1970-01-01
      相关资源
      最近更新 更多