Rspec - How to test a controller has included concerns

May 18th, 2015 in Ruby on Rails by Michael Cho

← All articles


A short Rspec snippet to verify the correct concerns have been included in a controller.

In Rails 4, developers were encouraged to move shared functionality into concerns and keep controllers as 'clean' as possible.

This is a short snippet to confirm the correct concerns are included in your controller. In this example, the test verifies the CommonRedirects concern is included.

 


describe PagesController do

  describe 'includes the correct concerns' do
    it { expect(controller.class.ancestors.include?(CommonRedirects)).to eq(true) }
  end

  # The rest of the controller test
end