Ruby FactoryGirl没有';t与build_属性的输出关联
我可能在这里做了一些非常愚蠢的事情,但我无法通过测试,因为的Ruby FactoryGirl没有';t与build_属性的输出关联,ruby,ruby-on-rails-4,rspec,factory-bot,rspec-rails,Ruby,Ruby On Rails 4,Rspec,Factory Bot,Rspec Rails,我可能在这里做了一些非常愚蠢的事情,但我无法通过测试,因为的FactoryGirl.attributes\u没有输出关系字段 这是我的项目工厂: FactoryGirl.define do factory :project do status 'active' loan_amount 5000 start_date Time.now end_date Time.now + 2.week.to_i description "Sample Project"
FactoryGirl.attributes\u没有输出关系字段
这是我的项目
工厂:
FactoryGirl.define do
factory :project do
status 'active'
loan_amount 5000
start_date Time.now
end_date Time.now + 2.week.to_i
description "Sample Project"
user
end
end
以下是我试图通过的测试:
let(:user) { FactoryGirl.create(:user) }
let(:project_attributes) { FactoryGirl.attributes_for(:project, user: user) }
it 'assigns a newly created project as @project' do
post :create, { project: project_attributes }
expect(assigns(:project)).to be_a(Project)
expect(assigns(:project)).to be_persisted
end
这是项目属性的输出:
{:status=>"active", :loan_amount=>5000, :start_date=>2015-12-21 12:07:32 -0800, :end_date=>2016-01-04 12:07:32 -0800, :description=>"Sample Project"}
这给了我一个错误:
@messages={:user=>["can't be blank"]}>
有什么想法吗?为什么FactoryGirl在使用属性时不输出用户?看起来您的:项目工厂定义有点不正确。如果项目属于用户,工厂需要说关联:用户
,如中所示
factory :project do
status 'active'
loan_amount 5000
start_date Time.now
end_date Time.now + 2.week.to_i
description "Sample Project"
association :user
end
这仍然不会将用户作为FactoryGirl的一部分输出。(:project,user:user)
user
的属性是关联:user,factory:user
TryFactoryGirl.build(:project,user:user)的可接受速记.attributes
问题是状态
是一个枚举,现在。attributes
正在将状态设置为1
导致错误:'1'不是有效状态
可能是的属性\u无法识别关联。当您改为user\u id:user.id
时会发生什么?