views:

249

answers:

2

Guys,

I'm having trouble understanding why I can't seem to stub this controller method :load_user, since all of my tests fail if I change the actual implementation of :load_user to not return and instance of @user.

Can anybody see why my stub (controller.stub!(:load_user).and_return(@user)) seems to fail to actually get called when RSpec makes a request to the controller?


require 'spec_helper'

describe TasksController do

  before(:each) do
    @user = Factory(:user)
    sign_in @user
    @task = Factory(:task)
    User.stub_chain(:where, :first).and_return(@user)
    controller.stub!(:load_user).and_return(@user)
  end

  #GET Index
  describe "GET Index" do

    before(:each) do
      @tasks = 7.times{Factory(:task, :user => @user)}
      @user.stub!(:tasks).and_return(@tasks)
    end

    it "should should find all of the tasks owned by a user" do
      @user.should_receive(:tasks).and_return(@tasks)
      get :index, :user_id => @user.id
    end

    it "should assign all of the user's tasks to the view" do
      get :index, :user_id => @user.id
      assigns[:tasks].should be(@tasks)      
    end
  end

  #GET New
  describe "GET New" do

    before(:each) do
      @user.stub_chain(:tasks, :new).and_return(@task)
    end

    it "should return a new Task" do
      @user.tasks.should_receive(:new).and_return(@task)
      get :new, :user_id => @user.id
    end
  end

  #POST Create
  describe "POST Create" do

    before(:each) do
      @user.stub_chain(:tasks, :new).and_return(@task)
    end

    it "should create a new task" do
     @user.tasks.should_receive(:new).and_return(@task)
      post :create, :user_id => @user.id, :task => @task.to_s
    end

    it "saves the task" do
      @task.should_receive(:save)
      post :create, :user_id => @user.id, :task => @task
    end

    context "when the task is saved successfully" do

      before(:each) do
        @task.stub!(:save).and_return(true)
      end

      it "should set the flash[:notice] message to 'Task Added Successfully'"do 
        post :create, :user_id => @user.id, :task => @task
        flash[:notice].should == "Task Added Successfully!"
      end

      it "should redirect to the user's task page" do
        post :create, :user_id => @user.id, :task => @task
        response.should redirect_to(user_tasks_path(@user.id))
      end
    end

    context "when the task isn't saved successfully" do

      before(:each) do
        @task.stub(:save).and_return(false)
      end

      it "should return to the 'Create New Task' page do" do
        post :create, :user_id => @user.id, :task => @task
        response.should render_template('new')
      end
    end
  end

  it "should attempt to authenticate and load the user who owns the tasks" do

    context "when the tasks belong to the currently logged in user" do

      it "should set the user instance variable to the currently logged in user" do
        pending 
      end

    end

    context "when the tasks belong to another user" do

      it "should set the flash[:notice] to 'Sorry but you can't view other people's tasks.'" do
        pending
      end

      it "should redirect to the home page" do
        pending
      end
    end
  end
end
class TasksController < ApplicationController
  before_filter :load_user 

  def index
    @tasks = @user.tasks
  end

  def new
    @task = @user.tasks.new
  end

  def create
    @task = @user.tasks.new
    if @task.save
      flash[:notice] = "Task Added Successfully!"
      redirect_to user_tasks_path(@user.id)
    else
      render :action => 'new'
    end
  end

  private

  def load_user
    if current_user.id == params[:user_id].to_i
      @user = User.where(:id => params[:user_id]).first 
    else
      flash[:notice] = "Sorry but you can't view other people's tasks."
      redirect_to root_path
    end
  end
end

Can anybody see why my stub doesnt' work? Like I said, my tests only pass if I make sure that load_user works, if not, all my tests fail which makes my think that RSpec isn't using the stub I created.

Thanks,

Joe

+1  A: 

you can also try to verify that the stub works by doing an assertion like

controller.current_user.should == @user
gerrit
+2  A: 

Stubbing out load_user breaks your tests because stubbing the method neuters it. When the controller calls load_user, it is no longer running your original code. It's now just returning whatever you specify in and_return(...) (which is getting returned to the ActionController callback stack, which ignores anything other than false).

Your controller code isn't using the return value of that method; it's using the variable instantiated within it. Since the original code for the load_user method isn't being run, the @user instance variable is never instantiated. (The @user variable in your tests is only visible to your tests.)

But with all the other stubs you have, I don't see any reason why you should need to stub out load_user at all. As long as you're stubbing current_user to return @user (which I assume is being done in the sign_in method), then there shouldn't be any need.

Ian
This is a great explanation, if I could vote it up more I would. Thank you so much, the explanation about returning @user vs instantiating @user really helped it click for me.
TheDelChop