Note that there are some explanatory texts on larger screens.

plurals
  1. PONested Resource testing RSpec
    primarykey
    data
    text
    <p>I have two models:</p> <pre><code>class Solution &lt; ActiveRecord::Base belongs_to :owner, :class_name =&gt; "User", :foreign_key =&gt; :user_id end class User &lt; ActiveRecord::Base has_many :solutions end </code></pre> <p>with the following routing:</p> <pre><code>map.resources :users, :has_many =&gt; :solutions </code></pre> <p>and here is the SolutionsController:</p> <pre><code>class SolutionsController &lt; ApplicationController before_filter :load_user def index @solutions = @user.solutions end private def load_user @user = User.find(params[:user_id]) unless params[:user_id].nil? end end </code></pre> <p>Can anybody help me with writing a test for the index action? So far I have tried the following but it doesn't work:</p> <pre><code>describe SolutionsController do before(:each) do @user = Factory.create(:user) @solutions = 7.times{Factory.build(:solution, :owner =&gt; @user)} @user.stub!(:solutions).and_return(@solutions) end it "should find all of the solutions owned by a user" do @user.should_receive(:solutions) get :index, :user_id =&gt; @user.id end end </code></pre> <p>And I get the following error:</p> <pre><code>Spec::Mocks::MockExpectationError in 'SolutionsController GET index, when the user owns the software he is viewing should find all of the solutions owned by a user' #&lt;User:0x000000041c53e0&gt; expected :solutions with (any args) once, but received it 0 times </code></pre> <p>Thanks in advance for all the help.</p> <p>Joe</p> <p>EDIT:</p> <p>Thanks for the answer, I accepted it since it got my so much farther, except I am getting another error, and I can't quite figure out what its trying to tell me:</p> <p>Once I create the solutions instead of build them, and I add the stub of the User.find, I see the following error:</p> <pre><code>NoMethodError in 'SolutionsController GET index, when the user owns the software he is viewing should find all of the solutions owned by a user' undefined method `find' for #&lt;Class:0x000000027e3668&gt; </code></pre>
    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