【发布时间】:2016-06-26 19:56:27
【问题描述】:
所以我的 rails_helper 中有这段代码
config.before(:suite) do
begin
FactoryGirl.lint
end
这让我很头疼。我有一个 User 类,它可以有几个这样的附加配置文件:
class User
has_one :student_profile, class_name: Student
has_one :employee_profile, class_name: Employee
end
现在的问题是,在用户注册期间,我需要根据正在注册的用户类型发送不同的电子邮件布局(我正在对他们的个人资料进行分类,并且根据“更强”的个人资料,我切换到适当的布局.
我已经覆盖了设计邮件程序以添加基于 main_profile 类型的布局
def layout_for_user(user)
case user.main_profile // user.employee_profile || user.student_profile || user
when Employee
'layouts/mailer/company'
when Student
'layouts/mailer/student'
else
fail ArgumentError, 'Unknown layout for profile'
end
end
在我的注册过程中,我确保在保存用户/发送确认之前至少构建了一种配置文件类型。
但看起来工厂女孩试图建造和拯救每种类型的工厂,所以我得到了很多user - Unknown layout for profile (ArgumentError)
有没有办法告诉 FactoryGirl.lint 跳过一些工厂?例如,没有任何个人资料的用户是没有意义的,但是仍然会生成错误
# rspec/factories/user.eb
FactoryGirl.define do
factory :user do
...
trait(:student) do
after(:build) do |user, evaluator|
user.student_profile = build(:student_profile,
user: user)
end
end
factory :student_user, traits: [:student]
end
这里我的user 工厂是某种抽象工厂,永远不应该单独实例化(否则会导致上述错误)有什么办法解决这个问题?我正在考虑评论这条线 FactoryGirl.lint 否则?
【问题讨论】:
-
我更喜欢跳过 linter 并在每个 model_spec 的开头进行测试。否则,您可以通过阅读文档github.com/thoughtbot/factory_girl/blob/master/… 跳过某些模型
标签: ruby-on-rails factory-bot rspec-rails ruby-on-rails-5