[rspec-users] stubbing controller methods, I'm stumped

Jonathan Linowes jonathan at parkerhill.com
Wed May 30 23:09:29 EDT 2007


I must be dense because I keep having trouble stubbing anything in a  
controller. I can stub models but not controller methods, whether  
directly defined in the controller file or in a module

Here's a small example:

# sandbox_controller.rb

class SandboxController < ApplicationController
   def someaction
     @value = somemethod
   end

   def somemethod
     return 10
   end
end

# sandbox_controller_spec.rb

require File.dirname(__FILE__) + '/../spec_helper'

describe SandboxController, " handling someaction" do
   before do
     SandboxController.stub!(:somemethod).and_return(99)
   end

   it "should get 99" do
     get :someaction
     assigns[:value].should equal(99)
   end
end

# console

$ script/spec spec/controllers/sandbox_controller_spec.rb --color -- 
format specdoc

SandboxController handling someaction
- should get 99 (FAILED - 1)

1)
'SandboxController handling someaction should get 99' FAILED
expected 99, got 10 (using .equal?)
./spec/controllers/sandbox_controller_spec.rb:10:
script/spec:4:

Finished in 0.13381 seconds

1 example, 1 failure


In real life I want to stub out the authentication methods in module / 
lib/authenticated_system.rb , or the before_filter method as  
suggested to me earlier so I can move forward with my spec'ing  
controllers that have authorization built in. This is getting painful.

linoj





More information about the rspec-users mailing list