开发者

how to write rspec for private method in controller with params

开发者 https://www.devze.com 2023-04-09 19:26 出处:网络
I have controller class ApplicationController < ActionController::Base def index end private def handle_login_sequence

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 Rs开发者_JS百科pec 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] 


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.

how to write rspec for private method in controller with params

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消