简体   繁体   English

如何使用 request.headers 测试控制器中任何操作的回调?

[英]How to test the callback for any actions in the controller using request.headers?

I have something like this controller:我有这样的控制器:

class ApiApplicationController < ActionController::API
  
  before_action :record_information_from_headers

  private

  def record_information_from_headers
    InformationSet.create(info: request.headers['INFO'])
  end
end

All other controllers are inherited from ApiApplicationController所有其他控制器都继承自ApiApplicationController

I want to test that my callback works before each method in child controllers.我想在子控制器中的每个方法之前测试我的回调是否有效。 And try to use Anonymous controller并尝试使用匿名控制器

require 'rails_helper'

RSpec.describe ApiApplicationController do
  controller(ApiApplicationController) do
    def index; end
  end

  let('INFO') { "some information" }

  it 'some' do
    get :index
    expect(InformationSet.last.info).to eq('some information')
  end
end

But, first of all, i have error: "NoMethodError: undefined method `controller' for RSpec::ExampleGroups::ApiApplicationController:Class"但是,首先,我有错误:“NoMethodError:RSpec::ExampleGroups::ApiApplicationController:Class 的未定义方法‘控制器’”

And secondly, how do I pass the information to the header ?其次,如何将信息传递给标题?

I've already read How to test ApplicationController method defined also as a helper method?我已经阅读了如何测试也定义为辅助方法的 ApplicationController 方法? and Rspec controller test for callback after_saveRspec 控制器测试回调 after_save

I would be grateful for any help)我将不胜感激任何帮助)

You don't have the type: describe ...Controller, type: :controller do你没有 type: describe ...Controller, type: :controller do

The Setting request headers section of the docs shows how to set request headers for controller specs.文档的设置请求标头部分显示了如何为控制器规范设置请求标头。

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

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