RSpec, stubbing nested resource methods

TheDelChop picture TheDelChop · Mar 31, 2010 · Viewed 8.4k times · Source

I've got two models:

class Solution < ActiveRecord::Base
  belongs_to :owner, :class_name => "User", :foreign_key => :user_id
end

class User < ActiveRecord::Base
  has_many :solutions
end

and I nest solutions within users like this:

ActionController::Routing::Routes.draw do |map|
  map.resources :users, :has_many => :solutions
end

and finally here's the action I"m trying to spec:

class SolutionsController < 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

My question is, how the heck do I Spec @user.solutions.find(params[:id])?

Here's my current spec:

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 => @user.id, :id => @solution.id
      end
    end
  end

But that gets me the following error:

1)Spec::Mocks::MockExpectationError in 'SolutionsController GET Show, when looking at a solution through a user's profile should find the specified solution'
<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)> received :find with unexpected arguments 
  expected: ("6")
  got: ("6", {:group=>nil, :having=>nil, :limit=>nil, :offset=>nil, :joins=>nil, :include=>nil, :select=>nil, :readonly=>nil, :conditions=>"\"solutions\".user_id = 34"})

Can anybody help me with how I can stub @user.solutions.new(params[:id])?

Answer

TheDelChop picture TheDelChop · Mar 31, 2010

Looks like I found my own answer, but I'm going to post it up here since I can't seem to find a whole lot about this on the net.

RSpec has a method called stub_chain: http://apidock.com/rspec/Spec/Mocks/Methods/stub_chain

which makes it easy to stub a method like:

@solution = @user.solutions.find(params[:id])

by doing this:

@user.stub_chain(:solutions, :find).with(@solution.id.to_s).and_return(@solution)

So then I can write an RSpec test like this:

it "should find the specified solution" do
  @user.solutions.should_receive(:find).with(@solution.id.to_s).and_return(@solution)
  get :show, :user_id => @user.id, :id => @solution.id
end

And my spec passes. However, I'm still learning here, so if anybody thinks my solution here is no good, please feel free to comment this and I try to get it totally right.

Joe