Jest + React + Rails:如何在数据库中创建对象进行测试?

问题描述 投票:1回答:1

我有一个带有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创建对象?还是我应该让所有人都使用水豚?

ruby-on-rails reactjs testing jestjs enzyme
1个回答
0
投票

您可以简单地使用Fixture,请参阅/ test / fixture文件夹。

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.