简体   繁体   English

当必须设置方法时,如何测试我的ApplicationController?

[英]How to test my ApplicationController, when a method has to be setup?

I am using rspec and having issues trying to test my ApplicationController. 我正在使用rspec并在尝试测试ApplicationController时遇到问题。

Is it possible to somehow set the values inside the controllers? 是否可以通过某种方式在控制器内部设置值? This is what I have now: 这就是我现在所拥有的:

class ApplicationController < ActionController::Base
  include CurrentUser
  before_action :load_account


  private
   def load_user
      @account = current_user.account if current_user.present?
   end
end

The included module just adds a current_user method that returns a User. 包含的模块仅添加了一个current_user方法,该方法返回一个User。

module CurrentUser
  def self.included(base)
    base.send :helper_method, :current_user
  end

  def current_user
    User.find_by(.....)  # returns a User object
  end
end

So when I am testing my controllers, I don't need to test the functionality of current_user.rb , can I somehow inject the value of current_user before my tests run? 因此,当我测试控制器时,不需要测试current_user.rb的功能,可以在测试运行之前以某种方式注入current_user的值吗?

example controller spec: 控制器规格示例:

require 'rails_helper'

RSpec.describe ProductsController, type: :controller do
  it "...." do
    get :new
    expect(response.body).to eq("hello")
  end
end

But currently any controller that expects a current_user is failing because it is nil. 但是当前所有期望current_user的控制器都失败了,因为它为nil。

You could setup a custom before :each in the config which stubs the current_user so it doesn't break your tests 您可以在配置中的:each之前设置一个自定义,以对current_user进行存根,这样就不会破坏您的测试

RSpec.configure do |config|
  config.before(:each, current_user_present: true) do
    account = double(:account)
    current_user = double(:current_user, account: account)
    expect(controller).to receive(:current_user).and_return(current_user)
    expect(current_user).to receive(:present?).and_return(true)
    expect(current_user).to receive(:account).and_return(account)
  end
end

RSpec.describe ProductsController, type: :controller, current_user_present: true do
  it "..." do
    #...
  end
end

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

相关问题 RSpec / Rails - 如何测试ApplicationController中的方法是否被调用(当我测试子类控制器时)? - RSpec / Rails - How do I test that a method in ApplicationController is called (when I'm testing a subclassed controller)? 如何测试ApplicationController方法也定义为辅助方法? - How to test ApplicationController method defined also as a helper method? 如何在ApplicationController中调用方法? - How to call a method in ApplicationController? 如何在我的gem中扩展ApplicationController? - How to extend ApplicationController in my gem? 我的Rails控制器测试未继承ApplicationController方法 - My Rails controller test is not inheriting the ApplicationController methods 如何在请求规范中存根ApplicationController方法 - How to stub ApplicationController method in request spec 在每次方法调用之前如何从ApplicationController调用方法? - How do a call a method from the ApplicationController before every method call? 如何使用RSpec在Rails中测试ApplicationController的动作之前 - How to test before action of ApplicationController in rails using RSpec 如何确定已从ApplicationController调用了哪个控制器? - How to determine which controller has been called from ApplicationController? 等号的ApplicationController方法 - ApplicationController Method with Equal Sign
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM