Note that there are some explanatory texts on larger screens.

plurals
  1. POAssociations with Factory Girl in Controller specs
    primarykey
    data
    text
    <p>I'm new to RSpec, and trying to get my head around using Factory Girl with associations in controller specs. The difficulty is:</p> <ul> <li>it's necessary to use "attributes_for" in functional tests</li> <li>attributes_for "<a href="https://github.com/thoughtbot/factory_girl/issues/359" rel="nofollow noreferrer">elides any associations</a>"</li> </ul> <p>So if I have models like this:</p> <pre><code>class Brand &lt; ActiveRecord::Base belongs_to :org validates :org, :presence =&gt; true end class Org &lt; ActiveRecord::Base has_many :brands end </code></pre> <p>And a factory like this:</p> <pre><code>FactoryGirl.define do factory :brand do association :org end end </code></pre> <p>This controller spec fails:</p> <pre><code>describe BrandsController do describe "POST create with valid params" do it "creates a new brand" do expect { post :create, brand: attributes_for(:brand) }.to change(Brand, :count).by(1) end end end </code></pre> <p>(And if I comment out "validates :org, :presence => true" it passes)</p> <p>There are a number of solutions suggested and I think I have been making simple errors which have meant that I have not been able to get any of them to work.</p> <p>1) Changing the factory to org_id per <a href="https://github.com/thoughtbot/factory_girl/issues/359" rel="nofollow noreferrer">a suggestion on this page</a> failed a number of tests with "Validation failed: Org can't be blank"</p> <pre><code>FactoryGirl.define do factory :brand do org_id 1002 end end </code></pre> <p>2) Using "symbolize_keys" looks promising. <a href="https://stackoverflow.com/questions/5103572/factorygirl-attributes-for-not-giving-me-associated-attributes">Here</a> and <a href="https://github.com/thoughtbot/factory_girl/issues/408" rel="nofollow noreferrer">here</a> it is suggested to use code like this: </p> <pre><code>(FactoryGirl.build :position).attributes.symbolize_keys </code></pre> <p>I'm not sure how to apply this in my case. Below is a guess that doesn't work (giving the error No route matches {:controller=>"brands", :action=>"{:id=>nil, :name=>\"MyString\", :org_id=>1052, :include_in_menu=>false, :created_at=>nil, :updated_at=>nil}"}): </p> <pre><code>describe BrandsController do describe "POST create with valid params" do it "creates a new brand" do expect { post build(:brand).attributes.symbolize_keys }.to change(Brand, :count).by(1) end end end </code></pre> <p><strong>Update</strong></p> <p>I almost got this working with Shioyama's answer below but got the error message:</p> <pre><code>Failure/Error: post :create, brand: build(:brand).attributes.symbolize_keys ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: id, created_at, updated_at </code></pre> <p>So following <a href="https://stackoverflow.com/questions/5103572/factorygirl-attributes-for-not-giving-me-associated-attributes/11438545#11438545">this question</a> I changed it to:</p> <pre><code>post :create, brand: build(:brand).attributes.symbolize_keys.reject { |key, value| !Brand.attr_accessible[:default].collect { |attribute| attribute.to_sym }.include?(key) } </code></pre> <p>Which worked!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload