hel*_*ion 5 minitest ruby-on-rails-3 factory-bot
我回去使用Fixtures.IMOP,固定装置比工厂更好; 更容易使用,更容易理解,更容易理解(没有魔力).我的建议:将测试库限制为非常基础(听DHH)...使用minitest和灯具.
在我的应用程序中,一个区有很多学校,一个学校有很多用途,一个用户有很多帐户,一个帐户有一个角色.为了创建完整的测试工厂,我需要创建一个贯穿工厂的用户和学校.我在最近的尝试中得到了"堆栈级太深"的错误.
我的user_test.rb
FactoryGirl.define do
factory :district do
name "Seattle"
end
factory :school do
association :primarycontact, factory: :user # expecting this to attach the user_id from factory :user as :primary contact_id in the school model
association :district, factory: :district # expecting this to attach the :district_id from the :district factory as :district_id in the school model
name "Test School"
end
factory :user do, aliases: [:primarycontact]
email "adam@example.com"
name "Who What"
username "wwhat"
password "123456"
password_confirmation { |u| u.password }
association :school, factory: :school # expecting this to create :school_id in the users model, using the :school factory
end
factory :role do
name "student"
end
factory :account do
association :user, factory: :user
association :role, factory: :role
end
end
Run Code Online (Sandbox Code Playgroud)
所以,我正在尝试做FactoryGirl.create(:account)...一些我期望创建一个帐户,其中包括上述工厂的用户和角色,用户与该学区相关联的用户.这不适合我.在失败的测试中,我得到"堆栈级太深"的错误.并且,我相信我在每个DatabaseCleaner.clean之前在每个新工厂之前清除测试数据库.
调用这些工厂的测试是:
describe "User integration" do
def log_em_in
visit login_path
fill_in('Username', :with => "wwhat")
fill_in('Password', :with => "123456")
click_button('Log In')
end
it "tests log in" do
user = FactoryGirl.create(:account)
log_em_in
current_path.should == new_user_path
end
end
Run Code Online (Sandbox Code Playgroud)
.
current_path.should == new_user_path returns unknown method error 'should'
Run Code Online (Sandbox Code Playgroud)
如何改进此代码以正确嵌套工厂并获取current_user以继续测试?
school.rb
belongs_to :district
belongs_to :primarycontact, :class_name => "User"
has_many :users, :dependent => :destroy
Run Code Online (Sandbox Code Playgroud)
user.rb
belongs_to :school
has_many :accounts, :dependent => :destroy
Run Code Online (Sandbox Code Playgroud)
district.rb
has_many :schools
Run Code Online (Sandbox Code Playgroud)
account.rb
belongs_to :role
belongs_to :user
Run Code Online (Sandbox Code Playgroud)
role.rb
has_many :accounts
has_many :users, :through => :accounts
Run Code Online (Sandbox Code Playgroud)
Chr*_*erg 12
您的基本问题是您的user工厂和school工厂之间存在循环依赖关系,这是因为您primarycontact在创建学校时创建了(用户),然后该用户创建了学校,等等.
您可以通过更改school在user工厂内定义关联的方式来解决此问题.在此之前,我建议使用关联的简写符号作为一般规则.所以替换这个:
factory :account do
association :user, factory: :user
association :role, factory: :role
end
Run Code Online (Sandbox Code Playgroud)
有了这个:
factory :account do
user
role
end
Run Code Online (Sandbox Code Playgroud)
使用这种简化,以下工厂将执行您想要的操作而不会产生任何循环依赖:
FactoryGirl.define do
factory :district do
name "Seattle"
end
factory :school do |school|
district
primarycontact
name "Test School"
after_build do |s|
s.primarycontact.school = s
end
end
factory :user do
email "adam@example.com"
name "Who What"
username "wwhat"
password "123456"
password_confirmation { |u| u.password }
school
end
factory :primarycontact, class: "User" do
# add any attributes you want the primarycontact user to have here
end
factory :role do
name "student"
end
factory :account do
user
role
end
end
Run Code Online (Sandbox Code Playgroud)
请注意,我所做的是primarycontact使用该class: "User"选项创建工厂.与user工厂不同,此工厂school默认情况下不创建,避免循环依赖.
然后在school工厂里,我使用after_build回调将学校本身分配给school协会primarycontact,而不是创建一所新学校(这导致了你工厂的问题).
希望有道理.请注意,更新版本的factory_girl中的回调语法已更改,有关详细信息,请参阅文档.
| 归档时间: |
|
| 查看次数: |
4538 次 |
| 最近记录: |