为 rspec 中的测试数据创建可重复使用的 class
Create reusable class for test data in rspec
我想创建一种快速创建测试数据的方法。
所以我创建了 FakeItemGroupCreation class,但我收到了这个错误
undefined method `create' for FakeItemGroupCreation:Class
如何使用创建方法?这是解决此问题的最佳做法吗?
require 'rails_helper'
RSpec.describe ItemGroupCreator, type: :model do
before(:each) do
@profile = create(:profile)
@category = create(:category)
end
class FakeItemGroupCreation
def self.create_same_category(profile, category, item_ids_pairs)
item_ids_pairs.each do |item_ids|
item_ids.each do |id|
create(:item, id: id, profile_id: profile.id, category: category)
end
# Creating other nested records..and perform calculations
end
end
end
it 'test with existing data A' do
existing_matrix = [ [4,5,6], [1,2,3] ]
FakeItemGroupCreation.create_same_category(@profile, @category, existing_matrix)
end
it 'test with existing data B' do
existing_matrix = [ [7,8,8], [4,5,2] ]
FakeItemGroupCreation.create_same_category(@profile, @category, existing_matrix)
end
您打算包装的是 FactoryBot 的 create
吗?如果是这样,请明确使用它
FactoryBot.create(:item, ...)
我想创建一种快速创建测试数据的方法。 所以我创建了 FakeItemGroupCreation class,但我收到了这个错误
undefined method `create' for FakeItemGroupCreation:Class
如何使用创建方法?这是解决此问题的最佳做法吗?
require 'rails_helper'
RSpec.describe ItemGroupCreator, type: :model do
before(:each) do
@profile = create(:profile)
@category = create(:category)
end
class FakeItemGroupCreation
def self.create_same_category(profile, category, item_ids_pairs)
item_ids_pairs.each do |item_ids|
item_ids.each do |id|
create(:item, id: id, profile_id: profile.id, category: category)
end
# Creating other nested records..and perform calculations
end
end
end
it 'test with existing data A' do
existing_matrix = [ [4,5,6], [1,2,3] ]
FakeItemGroupCreation.create_same_category(@profile, @category, existing_matrix)
end
it 'test with existing data B' do
existing_matrix = [ [7,8,8], [4,5,2] ]
FakeItemGroupCreation.create_same_category(@profile, @category, existing_matrix)
end
您打算包装的是 FactoryBot 的 create
吗?如果是这样,请明确使用它
FactoryBot.create(:item, ...)