Ruby on rails 消除RSpec和Factory Girl中的重复

Ruby on rails 消除RSpec和Factory Girl中的重复,ruby-on-rails,ruby,rspec,factory-bot,Ruby On Rails,Ruby,Rspec,Factory Bot,我正在学习使用RSpec 3.4进行测试,并使用Factory Girl 4.7进行夹具更换 我的代码中有很多重复,我真的不知道如何使它更干净 下面是一个代码示例: 用户许可证工厂.rb factory :user_licence do email 'user@email.com' trait :standard do product_id 1 licence_key '12345' end end factory :product do trait :pri

我正在学习使用RSpec 3.4进行测试,并使用Factory Girl 4.7进行夹具更换

我的代码中有很多重复,我真的不知道如何使它更干净

下面是一个代码示例:

用户许可证工厂.rb

factory :user_licence do
  email 'user@email.com'

  trait :standard do
    product_id 1
    licence_key '12345'
  end
end
factory :product do
  trait :primary_product do
    id 1
    name 'Primary Product'
  end
end
factory :site_licence do
  email 'user@email.com'

  trait :primary_product
    product_id 1
    uuid 'xxx'
  end
end
let(:user) { FactoryGirl.create(:user) }
let!(:main_product) { FactoryGirl.create(:product, :primary_product) }
let!(:licence) do
  FactoryGirl.create(:user_licence, :standard, email: user.email)
end
let!(:linked_licence) { FactoryGirl.create(:site_licence, :primary_product, email: user.email) }

it 'validates the licence key'
产品工厂.rb

factory :user_licence do
  email 'user@email.com'

  trait :standard do
    product_id 1
    licence_key '12345'
  end
end
factory :product do
  trait :primary_product do
    id 1
    name 'Primary Product'
  end
end
factory :site_licence do
  email 'user@email.com'

  trait :primary_product
    product_id 1
    uuid 'xxx'
  end
end
let(:user) { FactoryGirl.create(:user) }
let!(:main_product) { FactoryGirl.create(:product, :primary_product) }
let!(:licence) do
  FactoryGirl.create(:user_licence, :standard, email: user.email)
end
let!(:linked_licence) { FactoryGirl.create(:site_licence, :primary_product, email: user.email) }

it 'validates the licence key'
现场许可证工厂.rb

factory :user_licence do
  email 'user@email.com'

  trait :standard do
    product_id 1
    licence_key '12345'
  end
end
factory :product do
  trait :primary_product do
    id 1
    name 'Primary Product'
  end
end
factory :site_licence do
  email 'user@email.com'

  trait :primary_product
    product_id 1
    uuid 'xxx'
  end
end
let(:user) { FactoryGirl.create(:user) }
let!(:main_product) { FactoryGirl.create(:product, :primary_product) }
let!(:licence) do
  FactoryGirl.create(:user_licence, :standard, email: user.email)
end
let!(:linked_licence) { FactoryGirl.create(:site_licence, :primary_product, email: user.email) }

it 'validates the licence key'
api\u控制器\u规范rb

factory :user_licence do
  email 'user@email.com'

  trait :standard do
    product_id 1
    licence_key '12345'
  end
end
factory :product do
  trait :primary_product do
    id 1
    name 'Primary Product'
  end
end
factory :site_licence do
  email 'user@email.com'

  trait :primary_product
    product_id 1
    uuid 'xxx'
  end
end
let(:user) { FactoryGirl.create(:user) }
let!(:main_product) { FactoryGirl.create(:product, :primary_product) }
let!(:licence) do
  FactoryGirl.create(:user_licence, :standard, email: user.email)
end
let!(:linked_licence) { FactoryGirl.create(:site_licence, :primary_product, email: user.email) }

it 'validates the licence key'
这是一个简化版本,因为我有多个产品和许可证类型,然后产品与许可证绑定

当我需要测试API时,我需要创建用户、属于许可证的产品以及属于该产品的该用户的许可证。 每个用户可以拥有多个许可证,每个许可证属于一个独特的产品

我想要的工作流程是,创建一个用户并为该用户创建一个许可证。这样,即使我创建了10个许可证,也只能创建一个产品


谢谢

我必须查看实际规格才能更好地了解情况,但根据规格的分组方式,您可以做的只是在每次测试开始时在许可证上重新分配产品,因此您只需要有一个用户和一个许可证。规格都在问题中,你还需要什么?我指的是你的规格中包含expect语句的部分。我必须查看实际规格才能更好地了解,但是,根据规格的分组方式,您可以做的只是在每次测试开始时重新分配许可证上的产品,这样您只需要一个用户和一个许可证。规格都在问题中,您还需要什么?我指的是规格中包含expect语句的部分。