我有这些模型:
class User < ActiveRecord::Base
has_many :company_users, :dependent => :delete_all
class Company < ActiveRecord::Base
has_many :company_users, :dependent => :delete_all
has_many :users, :through => :company_users
class CompanyUser < ActiveRecord::Base
belongs_to :company
belongs_to :user
我使用这个辅助函数来创建和登录:
def create_company_admin
company_user = FactoryGirl.create(:company_user, :has_shopper, :has_company, :admin)
user = User.first
login_as user, scope: :user
company_user
end
这是实际的测试
feature 'user listing' do
background do
company_user = create_company_admin
end
scenario 'getting user list' do
visit '/companies'
page.has_content? company_user.user.name
end
end
这一直有效,直到他调用:company_user.user.name 似乎不存在。在那里我得到未定义的局部变量或方法“company_user”
这是工厂:
factory :company_user, class: CompanyUser do
authorized true
trait :admin do
admin true
end
trait :not_admin do
admin false
end
trait :has_shopper do
association :user, factory: :shopper
end
trait :has_company do
association :company, factory: :company
end
end
编辑:
这是购物者的工厂(这是一种用户)
factory :shopper, class: User do
username "user"
email "mail@mai.co"
password "password1"
after(:create) do |u|
u.skip_confirmation!
u.save!
end
end
如何在测试中获取用户、公司和公司用户?