0

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.

Community
  • 1
  • 1
boddhisattva
  • 6,908
  • 11
  • 48
  • 72
  • Kinda silly question, but are you sure you've got the rspec stuff set up correctly? The `stub!` methods should be where you're looking for them... – Matt Jones Feb 15 '14 at 05:00
  • Yes, I'm sure it's setup correctly as I'm able to run other test cases(the ones on models). Why do u think the question is silly ? – boddhisattva Feb 15 '14 at 05:16
  • Interestingly, I jus observed tht in d Sessions Ctrller(in app) tht takes care of login n logout, they're actually via a skip_before_filter by passing the 'login_is_required' action which is a before_filter in The Application Ctrller.. I'm wondering if I add a skip_before_filter to my Ctrller_NameCtrller that shud help me call the update method without any login issues or otherwise I was wonderin if I can call d Sessions ctrller create method jus bfore callin the put methd in my current ctrller. But I'm not sure if it'll work n also I don knw how 2 call sessions create within above rspec code – boddhisattva Feb 15 '14 at 05:25
  • Apologies for the confusion - *my* question was kinda silly, sort of an "are you sure the computer's plugged in?" thing. I asked because the RSpec 1.2.9 source definitely includes the stub! method, so something weird is going on with how it's being loaded. I still have no idea what's actually going wrong, but can you post a stack trace of some of these failures? – Matt Jones Feb 15 '14 at 16:39

0 Answers0