简体   繁体   English

如何用params在控制器中为私有方法编写rspec

[英]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 我想为这个私有方法编写Rspec

@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: 通过上面的代码,我可以调用handle_login_sequence方法,但我不知道如何传递以下内容:

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. 不要忘记关于控制器的黑盒子比喻。

黑匣子测试图

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. 黑盒测试将帮助您确保在未直接测试私有方法的情况下未破坏控制器功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM