【问题标题】:Jest + React + Rails: how to create objects in database for testing?Jest + React + Rails:如何在数据库中创建对象进行测试?
【发布时间】:2020-03-23 18:24:06
【问题描述】:

我有一个带有 React 前端的 Rails 应用程序。我想使用 Jest 进行功能测试,因为从事这个项目的其他人都专注于 React。前后通过API相互联系。

我们有一个页面,其中列出了许多电话代码。这些代码存在于数据库中。我想测试一下索引页是否真的在渲染电话代码。

如果我使用的是 RSpec + Capybara,我会编写这样的功能测试:

before do
  phone_code = PhoneCode.create(prefix: 495, description: 'Moscow')
  another_code = PhoneCode.create(prefix: 8692, description: 'Sevastopol')
end

describe 'index' do
  it 'lists all phone codes in database' do
    visit 'api/v1/phone_codes'
    expect(page.body).to include(phone_code.description)
    expect(page.body).to include(another_code.description)
  end
end

但是,我不知道如何在 React 及其测试框架中的数据库中创建东西;除了,也许,通过创建形式,这在我们的应用程序中不存在。

那么,我该怎么办?有没有办法从 Jest 创建对象?还是我应该让每个人都使用 Capybara?

【问题讨论】:

    标签: ruby-on-rails reactjs testing jestjs enzyme


    【解决方案1】:

    你可以简单地使用 Fixtures 查看 /test/fixture 文件夹。

    考虑查看https://guides.rubyonrails.org/testing.html#the-test-environment

    https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

    当您为模型使用生成器时,骨架应该已经存在。

    【讨论】:

      猜你喜欢
      • 2021-07-21
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 2020-08-17
      • 2021-11-07
      • 2019-06-08
      • 2020-10-03
      • 2020-07-04
      相关资源
      最近更新 更多