Search code examples
rubyunit-testingrspecauthlogic

Testing @current_user method using RSpec


I've been trying to do this for a couple of days now, but I can't figure it out. I have the following code in my controller:

@some_object = @current_user.some_method

In my spec, I want to attach a should_receive hook on that method, but I can't make it work. I've tried all of these, but none of them work:

assigns[:current_user].should_receive(:some_method).at_least(:once) # expected 1, got 0
User.should_receive(:some_method).at_least(:once) # expected 1, got 0

How is the correct way of testing this? I'm running this in my spec, and login is working:

setup :activate_authlogic
...
UserSession.create(users(:rune))

Thanks!


Solution

  • One example comes from the Ruby on Rails Tutorial. Rather than setting and reading @current_user directly, it defines two helper methods:

    def current_user=(user)
      @current_user = user
    end
    
    def current_user
      @current_user
    end
    

    Later, they access this method in the tests using the controller method:

    def test_sign_in(user)
      controller.current_user = user
    end
    

    Using this methodology, you should be able to use

    controller.current_user.should_receive(:some_method).at_least(:once)