RSpec 带有 FactoryGirl 显式主题
RSpec with FactoryGirl explicit subject
我在 Rails 的 Ruby 环境中使用 RSpec 和 FactoryGirl 进行测试。
我想指定我的工厂如下:
factory :user do
role # stub
factory :resident do
association :role, factory: :resident_role
end
factory :admin do
association :role, factory: :admin_role
end
end
我想在我的规范中做这样的事情:
require 'rails_helper'
RSpec.describe User, type: :model do
context "all users" do
# describe a user
# subject { build(:user) }
# it { is_expected.to be_something_or_do_something }
end
context "residents" do
# describe a resident
# subject { build(:resident) }
# it { is_expected.to be_something_or_do_something }
end
context "admins" do
# describe a admin
# subject { build(:admin) }
# it { is_expected.to be_something_or_do_something }
end
end
可以通过显式设置 subject
来完成吗?当我这样做时,我不断收到重复角色错误。
如有任何意见或建议,将不胜感激!
我认为您不想阻止它们自动加载,而且我实际上不确定您不允许它们加载的用例是什么?
RSpec automagically fetches the factory for a spec
Rspec 我相信当你的规范助手加载时,将所有工厂加载到内存中。因为您使用工厂继承,所以您只是在测试之前将它们中的每一个加载到内存中 运行,没有调用任何东西,也没有创建或构建任何对象。它们已准备好在您的测试中使用。
您是否遇到了特定的错误,或者在某些情况下我没有看到您需要?
But this causes the user_spec.rb to use the :user factory.
不,不是。假设您正确配置了 FactoryGirl,RSpec 可以在任何测试文件中使用您想要的任何工厂 "on demand"。配置方面,在 rails_helper.rb
中将其放入:
RSpec.configure do |config|
# ...
config.include FactoryGirl::Syntax::Methods
# ...
end
然后,在您的规范文件中:
require 'rails_helper'
RSpec.describe User, type: :model do
context "all users" do
let(:user) { create(:user) }
it 'is a user' do
# Here `user` is going to be a user factory
expect(user.unit).not_to be_present
end
end
context "residents" do
let(:user) { create(:resident) }
it 'is a resident' do
# Here `user` is going to be a resident factory
expect(user.unit).to be_present
end
end
context "admins" do
let(:user) { create(:admin) }
it 'is an admin' do
# Here `user` is going to be an admin factory
expect(user.role).to be('admin_role')
end
end
end
简而言之,您可以对存在于以下任一路径中的任何工厂定义使用 create(<factory_name>)
:
test/factories.rb
spec/factories.rb
test/factories/*.rb
spec/factories/*.rb
请注意,如果您没有在 RSpec.configure
中放置 config.include FactoryGirl::Syntax::Methods
,您仍然可以创建任何工厂,方法是 FactoryGirl.create(<factory_name>)
而不是 create(<factory_name>)
。
我在这里找到了问题的解决方案:https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associations
我需要在我的用户工厂中使用的是 association :role, factory: :role, strategy: :build
我在 Rails 的 Ruby 环境中使用 RSpec 和 FactoryGirl 进行测试。
我想指定我的工厂如下:
factory :user do
role # stub
factory :resident do
association :role, factory: :resident_role
end
factory :admin do
association :role, factory: :admin_role
end
end
我想在我的规范中做这样的事情:
require 'rails_helper'
RSpec.describe User, type: :model do
context "all users" do
# describe a user
# subject { build(:user) }
# it { is_expected.to be_something_or_do_something }
end
context "residents" do
# describe a resident
# subject { build(:resident) }
# it { is_expected.to be_something_or_do_something }
end
context "admins" do
# describe a admin
# subject { build(:admin) }
# it { is_expected.to be_something_or_do_something }
end
end
可以通过显式设置 subject
来完成吗?当我这样做时,我不断收到重复角色错误。
如有任何意见或建议,将不胜感激!
我认为您不想阻止它们自动加载,而且我实际上不确定您不允许它们加载的用例是什么?
RSpec automagically fetches the factory for a spec
Rspec 我相信当你的规范助手加载时,将所有工厂加载到内存中。因为您使用工厂继承,所以您只是在测试之前将它们中的每一个加载到内存中 运行,没有调用任何东西,也没有创建或构建任何对象。它们已准备好在您的测试中使用。
您是否遇到了特定的错误,或者在某些情况下我没有看到您需要?
But this causes the user_spec.rb to use the :user factory.
不,不是。假设您正确配置了 FactoryGirl,RSpec 可以在任何测试文件中使用您想要的任何工厂 "on demand"。配置方面,在 rails_helper.rb
中将其放入:
RSpec.configure do |config|
# ...
config.include FactoryGirl::Syntax::Methods
# ...
end
然后,在您的规范文件中:
require 'rails_helper'
RSpec.describe User, type: :model do
context "all users" do
let(:user) { create(:user) }
it 'is a user' do
# Here `user` is going to be a user factory
expect(user.unit).not_to be_present
end
end
context "residents" do
let(:user) { create(:resident) }
it 'is a resident' do
# Here `user` is going to be a resident factory
expect(user.unit).to be_present
end
end
context "admins" do
let(:user) { create(:admin) }
it 'is an admin' do
# Here `user` is going to be an admin factory
expect(user.role).to be('admin_role')
end
end
end
简而言之,您可以对存在于以下任一路径中的任何工厂定义使用 create(<factory_name>)
:
test/factories.rb
spec/factories.rb
test/factories/*.rb
spec/factories/*.rb
请注意,如果您没有在 RSpec.configure
中放置 config.include FactoryGirl::Syntax::Methods
,您仍然可以创建任何工厂,方法是 FactoryGirl.create(<factory_name>)
而不是 create(<factory_name>)
。
我在这里找到了问题的解决方案:https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associations
我需要在我的用户工厂中使用的是 association :role, factory: :role, strategy: :build