次のように、コントローラーのテストで FactoryGirl.attributes_for を使用したいと考えています。
it "raise error creating a new PremiseGroup for this user" do
expect {
post :create, {:premise_group => FactoryGirl.attributes_for(:premise_group)}
}.to raise_error(CanCan::AccessDenied)
end
...しかし、 #attributes_for が :user_id 属性を省略しているため、これは機能しません。#create
との違いは次の#attributes_for
とおりです。
>> FactoryGirl.create(:premise_group)
=> #<PremiseGroup id: 3, name: "PremiseGroup_4", user_id: 6, is_visible: false, is_open: false)
>> FactoryGirl.attributes_for(:premise_group)
=> {:name=>"PremiseGroup_5", :is_visible=>false, :is_open=>false}
:user_id が にないことに注意してください#attributes_for
。これは予想される動作ですか?
FWIW、私の工場ファイルには:premise_group
と の定義が含まれています:user
:
FactoryGirl.define do
...
factory :premise_group do
sequence(:name) {|n| "PremiseGroup_#{n}"}
user
is_visible false
is_open false
end
factory :user do
...
end
end