vinothini
vinothini

Reputation: 2604

how to write rspec for private method in controller with params

I have controller

class ApplicationController < ActionController::Base
  def index
  end

  private

    def handle_login_sequence
      username = params[:userName]
      password = params[:password]

      cookies[:locale]  = params[:locale]
      remember          = params[:remember]

      username_locked   = User.locked_username?(username)
      user = User.authenticate(username, password)

      if user && user.has_portal_access?
        case user.account_status
          when AccountStatus::Active
            flash[:error] =  'login'
        end
      end
    end

end

I want to write Rspec for this private method

@controller = ApplicationController.new
@controller.send(:handle_login_sequence)

By the above code I can call handle_login_sequence method but I don't know how to pass the below:

params[:userName], params[:password], params[:locale], params[:remember] 

Upvotes: 5

Views: 2947

Answers (1)

iafonov
iafonov

Reputation: 5192

You shouldn't test private methods of a controller directly. Instead, test the controller action that uses this method.

Don't forget about black box metaphor with regards to your controllers.

Black Box testing diagram

If you test private methods, you'll have to rewrite tests when you want to change the just the implementation and not the interface. Black box tests will help you to make sure that you haven't broken your controller functionality without directly testing the private methods.

Upvotes: 1

Related Questions