1

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

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"

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? and Rspec controller test for callback after_save

I would be grateful for any help)

Senonik
  • 21
  • 2

1 Answers1

0

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

The Setting request headers section of the docs shows how to set request headers for controller specs.

smathy
  • 26,283
  • 5
  • 48
  • 68