Note that there are some explanatory texts on larger screens.

plurals
  1. POForce controller to use current_user with mocking
    text
    copied!<p>I am trying to specify in my RSpec tests that my controller should use <code>current_user.projects.find()</code> instead of <code>Project.find()</code> I am using the Mocha mocking framework and was trying something like this:</p> <pre><code>controller.current_user.projects.expects(:find).returns(@project) </code></pre> <p>I have already mocked out <code>controller.stubs(:current_user).returns(@profile)</code></p> <p>This test passes with this even when I use the <code>Project.find()</code> implementation. How can I test that my controller is calling off of the correct object?</p> <p>Edit (adding additional code):</p> <p>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 <code>current_user</code></p> <p>Action in the controller:</p> <pre><code>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 </code></pre> <p>This is the test that is not checking that the <code>cardsets</code> method was called off the <code>current_user</code> object.</p> <p>Current Test:</p> <pre><code>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 =&gt; @project.id, :id =&gt; @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 =&gt; @project.id, :id =&gt; @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 </code></pre>
 

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