Note that there are some explanatory texts on larger screens.

plurals
  1. PORSpec, stubbing nested resource methods
    text
    copied!<p>I've got 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>and I nest solutions within users like this:</p> <pre><code>ActionController::Routing::Routes.draw do |map| map.resources :users, :has_many =&gt; :solutions end </code></pre> <p>and finally here's the action I"m trying to spec:</p> <pre><code>class SolutionsController &lt; ApplicationController before_filter :load_user def show if(@user) @solution = @user.solutions.find(params[:id]) else @solution = Solution.find(params[:id]) end end private def load_user @user = User.find(params[:user_id]) unless params[:user_id].nil? end end </code></pre> <p>My question is, how the heck do I Spec <code>@user.solutions.find(params[:id])</code>? </p> <p>Here's my current spec:</p> <pre><code>describe SolutionsController do before(:each) do @user = Factory.create(:user) @solution = Factory.create(:solution) end describe "GET Show," do before(:each) do Solution.stub!(:find).with(@solution.id.to_s).and_return(@solution) User.stub!(:find).with(@user.id.to_s).and_return(@user) end context "when looking at a solution through a user's profile" do it "should find the specified solution" do Solution.should_receive(:find).with(@solution.id.to_s).and_return(@solution) get :show, :user_id =&gt; @user.id, :id =&gt; @solution.id end end end </code></pre> <p>But that gets me the following error:</p> <pre><code>1)Spec::Mocks::MockExpectationError in 'SolutionsController GET Show, when looking at a solution through a user's profile should find the specified solution' &lt;Solution(id: integer, title: string, created_at: datetime, updated_at: datetime, software_file_name: string, software_content_type: string, software_file_size: string, language: string, price: string, software_updated_at: datetime, description: text, user_id: integer) (class)&gt; received :find with unexpected arguments expected: ("6") got: ("6", {:group=&gt;nil, :having=&gt;nil, :limit=&gt;nil, :offset=&gt;nil, :joins=&gt;nil, :include=&gt;nil, :select=&gt;nil, :readonly=&gt;nil, :conditions=&gt;"\"solutions\".user_id = 34"}) </code></pre> <p>Can anybody help me with how I can stub <code>@user.solutions.new(params[:id])</code>?</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