Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: Unable to initialize has_many :through association from within unit test using model class name
    primarykey
    data
    text
    <p>I've been struggling to understand why I can easily use seeds.rb to create new Users and their default associations, but when running individual a unit test causes errors. I've tried to get around calling 'Role' as it causes errors in the unit test. I am relatively new to unit testing, but have been using Rails for several years already. Although it all works, I want the tests to come out error free. </p> <blockquote> <p>ruby 1.8.7 <br> gem 1.3.7 <br>Rails 2.3.8</p> </blockquote> <p>app/models/user.rb</p> <pre><code>class User &lt; ActiveRecord::Base has_many :user_roles, :dependent =&gt; :destroy has_many :roles, :through =&gt; :user_roles, :uniq =&gt; true, :order =&gt; :id has_one :contact, :as =&gt; :owner, :dependent =&gt; :destroy attr_accessor :password before_save :build_default_associations, :if =&gt; :new_record? def build_default_associations update_encrypted_password build_contact(:email =&gt; name) user_roles.build(:role_id =&gt; Role.find_by_name("subscriber")id ) # # the below also works in seeds.rb but causes a different error # roles &lt;&lt; Role.find_by_name("subscriber") # ActiveRecord::AssociationTypeMismatch: Role(#2162845660) expected, got NilClass(#2148404100) # end </code></pre> <p>db/seeds.rb </p> <pre><code># This places the default Role in the db Role.create({:name =&gt; "subscriber", :description =&gt; "This will be the default"}) # associations initialize and save with User.create(:name =&gt; "valid@email.com", :password =&gt; "abcde") </code></pre> <p>test/unit/user_test.rb</p> <pre><code># assertions DO NOT fail, but raise the error below! def test_should_create_user_and_add_default_role user = User.create(:name =&gt; 'ok@good.org', :password =&gt; 'abcde') #minimum length password assert !user.new_record? assert user.roles.exists?("subscriber") end </code></pre> <p>ruby test/unit/user_test.rb</p> <blockquote> <p>1) Error: <br> test_should_create_user_and_add_default_role(UserTest): <br>RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id <br>app/models/user.rb:31:in 'build_default_associations' <br>unit/user_test.rb:31:in `test_should_create_user_and_add_default_role'</p> </blockquote> <p>Any thoughts? I can't seem to find any best practices that prohibit this...</p>
    singulars
    1. This table or related slice is empty.
    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.
    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