Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting a test for a Rails 3 model method that involves multiple associations
    text
    copied!<p>I'm new to the concept of test-driven development (and fairly new to Rails). I'm stuck with how to approach writing a test for my application.</p> <p>As a backgrounder, the web app is a peer evaluation tool where individual groups are assigned surveys where they are asked to evaluate their peers. So a sample question would look like this:</p> <pre><code>Rate your peers on time management. - Peer 1: __ - Peer 2: __ - Yourself: __ </code></pre> <p>How I have this set up internally is as so:</p> <p>I have a <code>Survey</code> model with a <code>has_many</code> association to an <code>Assignment</code> model (which controls the visibility of surveys among groups), which acts as a join model for <code>Survey</code> and a <code>Group</code> model. In the <code>Group</code> model there is a <code>has_many</code> association with <code>User</code>s. </p> <p>Because multiple groups can be assigned the same survey, I have to find out which <code>Group</code> a certain user is asked to evaluate. (So the survey would be the same, but the people that show up as in the sample above would change from user to user). </p> <p>I have decided to create a method, <code>assignees_for_user(user)</code>, which would return, essentially, the user's group(s) that have been assigned to the survey.</p> <p>I want to be able to unit test this method. I am using rspec and factory_girl so far. I have a few questions:</p> <ol> <li>Is my approach even correct design-wise?</li> <li>Should I test this? If so, I'll need to create instances for <code>Assignment</code>, <code>Group</code>, and <code>User</code>, right? </li> <li>What's the best way I can stub/mock these instances without persisting them onto the database? Or will I have to? </li> </ol> <p>Thanks! </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