I'm working on a Rails 2.1 app. This app uses 'rspec', '1.2.9' and 'rspec-rails', '1.2.9' . This app uses restful authentication plugin. There is a :login_required before filter method that I have in my Application Controller which basically does the authentication check before allowing access to controller method.
I referred to some older stack overflow(so) questions where users had similar issues, PFB the errors that I got when I tried each of the recommended solutions with respect to those questions.
1 How to stub Restul-authentication's current_user method?
The error I got -
undefined method `stub!' for #<User:0xf5a9c07c>
2 Rails, Restful Authentication & RSpec - How to test new models that require authentication
With respect to this question, I added the below line of code as given in the second answer to the same.
The error I got when I added the above line is -
controller.stub!(:authenticate).and_return(true)
undefined method `stub!' for Controller_NameController:Class
On closer observation, I find that something's wrong with why my test cases aren't able to pick up the stub method.
Here's my code below that I've written to call a specific action within a controller.
Here Controller_Name refers to a controller name like Users controller and in that sense the Controller_NameController basically will stand for UsersController.
require 'spec_helper'
describe Controller_NameController do
describe "What action to test" do
describe "what specific field to update as part of action" do
before do
#Controller_NameController.stub!(:login_required).and_return(true) #this line throws error wrt point 2
#does some stuff
end
it "should update the flag by calling the update method" do
#@user_session = login_as(stub_model(User))
#UserSession.stubs(:new).returns(@user_session) - these two lines throws errors wrt point 1
put :update_flag, :id => @obj.id, :obj => {:flag_name => 1}
@obj.updated_at.hour.should == Time.now.hour
end
end
end
end
I'm not sure what's exactly wrong here. Any pointers on how to get this working would be very helpful.
Thank you.