Senonik
Senonik

Reputation: 31

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

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)

Upvotes: 1

Views: 469

Answers (1)

smathy
smathy

Reputation: 27971

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.

Upvotes: 0

Related Questions