【问题标题】:testing - accessing Seed data in Cypress测试 - 在 Cypress 中访问种子数据
【发布时间】:2021-01-16 06:22:18
【问题描述】:

我想在测试期间访问种子数据库

我的设置:

我的测试种子包含:

FactoryBot.create_list(:user, 10)

我的用户工厂看起来像:

  factory :user { email { Faker::Internet.unique.email } }

(所以我无法事先知道他们的电子邮件地址)

我的目标

在我的测试中,我想做一些类似的事情:

    cy.get('body')
      .should('contain', `${user.last.email}`)

有没有办法在赛普拉斯测试中实现该结果?

【问题讨论】:

    标签: ruby-on-rails cypress e2e-testing seeding


    【解决方案1】:

    Cypress Real World Appa payment application to demonstrate real-world usage of Cypress testing methods, patterns, and workflows 演示了如何通过以与语言和框架无关的方式实现的远程命令访问种子数据。

    这是使用赛普拉斯命令cy.database 完成的,该命令调用Cypress Task depending upon the operation (filter or find)

    任务向test API endpoint(即only exposed for the test environment)发出一个API请求。

    此技术可用于从远程或本地实例访问种子数据,并可用于驱动测试,如 the notifications spec 中所示。

    【讨论】:

      【解决方案2】:

      您可以使用https://github.com/shakacode/cypress-on-rails gem 来完成这项工作。

      设置 gem 后,您的代码可能如下所示:

       cy.appFactories([
         ['create_list', 'user', 9],
         ['create', 'user', email: 'myuser@email.com'],
       ])
       cy.visit('/users')
       cy.get('body').should('contain', 'myuser@email.com')
      

      【讨论】:

        猜你喜欢
        • 2021-11-05
        • 1970-01-01
        • 2021-09-14
        • 2022-06-29
        • 2015-04-11
        • 1970-01-01
        • 2023-01-20
        • 2016-11-07
        • 2020-05-02
        相关资源
        最近更新 更多