views:

41

answers:

1

I am trying to specify in my RSpec tests that my controller should use current_user.projects.find() instead of Project.find() I am using the Mocha mocking framework and was trying something like this:

controller.current_user.projects.expects(:find).returns(@project)

I have already mocked out controller.stubs(:current_user).returns(@profile)

This test passes with this even when I use the Project.find() implementation. How can I test that my controller is calling off of the correct object?

Edit (adding additional code):

I have Projects and Tasks, Project have many tasks. This is the show method for displaying a task in a project that is owned by current_user

Action in the controller:

def show
    @project = current_user.projects.find_by_id(params[:cardset_id])

    if @project.nil?
      flash[:notice] = "That project doesn't exist. Try again."
      redirect_to(projects_path)
    else
      @task = @project.tasks.find_by_id(params[:id])
    end
  end

This is the test that is not checking that the cardsets method was called off the current_user object.

Current Test:

context "with get to show" do
  context "with valid project" do
    before(:each) do
      @project = Factory(:project)
      @task = Factory(:task)
      @profile = @project.profile
      ApplicationController.stubs(:require_user).returns(true)
      controller.stubs(:current_user).returns(@profile)

      Project.stubs(:find_by_id).returns(@project)
      @project.tasks.stubs(:find_by_id).returns(@task)
      get :show, :project_id => @project.id, :id => @task.id
    end

    it "should assign task" do
      assigns[:task].should_not be_nil
    end

    it "should assign project" do
      assigns[:project].should_not be_nil
    end
  end

  context "with invalid project" do
    before(:each) do
      Project.stubs(:find_by_id).returns(nil)
      get :show, :project_id => @project.id, :id => @task.id
    end

    it "should set flash" do
      flash[:notice].should match(/doesn't exist/i)
    end

    it "should redirect" do
      response.should redirect_to(cardsets_url)
    end
  end
end
A: 

Based on the little you've told us, I think you need:

@profile.expects(:find).returns(@project)
Bryan Ash
I updated my question with more of the code I am using.
trobrock
This is close to what I needed.... `@profile.cardsets.expects(:find_by_id).returns(@project)`
trobrock