Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I test that my table design implements my associations properly?
    text
    copied!<p>I have written my basic models and defined their associations as well as the migrations to create the associated tables. </p> <p><strong>EDIT - Adding emphasis to what I specifically want to test.</strong></p> <p><strong>I want to be able to test:</strong></p> <ul> <li><strong>The associations are configured as intended</strong></li> <li><strong>The table structures support the associations properly</strong></li> </ul> <p>I've written FG factories for all of my models in anticipation of having a complete set of test data but I can't grasp how to write a spec to test both belongs_to and has_many associations. </p> <p>For example, given an Organization that has_many Users I want to be able to test that my sample Organization has a reference to my sample User.</p> <p>Organization_Factory.rb:</p> <pre><code>Factory.define :boardofrec, :class =&gt; 'Organization' do |o| o.name 'Board of Recreation' o.address '115 Main Street' o.city 'Smallville' o.state 'New Jersey' o.zip '01929' end Factory.define :boardofrec_with_users, :parent =&gt; :boardofrec do |o| o.after_create do |org| org.users = [Factory.create(:johnny, :organization =&gt; org)] end end </code></pre> <p>User_Factory.rb:</p> <pre><code>Factory.define :johnny, :class =&gt; 'User' do |u| u.name 'Johnny B. Badd' u.email 'jbadd@gmail.com' u.password 'password' u.org_admin true u.site_admin false u.association :organization, :factory =&gt; :boardofrec end </code></pre> <p>Organization_spec.rb:</p> <pre><code>... it "should have the user Johnny B. Badd" do boardofrec_with_users = Factory.create(:boardofrec_with_users) boardofrec_with_users.users.should include(Factory.create(:johnny)) end ... </code></pre> <p>This example fails because the Organization.users list and the comparison User :johnny are separate instances of the same Factory.</p> <p>I realize this doesn't follow the BDD ideas behind what these plugins (FG, rspec) seemed to be geared for but seeing as this is my first rails application I'm uncomfortable moving forward without knowing that I've configured my associations and table structures properly.</p>
 

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