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.i开发者_如何转开发d
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
Based on the little you've told us, I think you need:
@profile.expects(:find).returns(@project)
精彩评论