Rspec, Rails: how to test private methods of controllers?

I have controller:

class AccountController < ApplicationController
  def index
  end

  private
  def current_account
    @current_account ||= current_user.account
  end
end

How to test private method current_account with rspec?

P.S. I use Rspec2 and Ruby on Rails 3


Solution 1:

Use #instance_eval

@controller = AccountController.new
@controller.instance_eval{ current_account }   # invoke the private method
@controller.instance_eval{ @current_account }.should eql ... # check the value of the instance variable

Solution 2:

I use send method. Eg:

event.send(:private_method).should == 2

Because "send" can call private methods

Solution 3:

Where is the current_account method being used? What purpose does it serve?

Generally, you don't test private methods but rather test the methods that call the private one.

Solution 4:

You should not test your private methods directly, they can and should be tested indirectly by exercising the code from public methods.

This allows you to change the internals of your code down the road without having to change your tests.

Solution 5:

You could make you private or protected methods as public:

MyClass.send(:public, *MyClass.protected_instance_methods) 
MyClass.send(:public, *MyClass.private_instance_methods)

Just place this code in your testing class substituting your class name. Include the namespace if applicable.