Rspec - How to test a controller has included concerns

May 18th, 2015 in  by Michael Cho

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) }

  # The rest of the controller test

Other articles you may like

Understanding Time, DateTime, and timezones in Rails
Sep 13th, 2015
How to split Rails routes.rb into multiple smaller files
Apr 27th, 2015
Rails 4 Conditional Page Cache for Logged In Users
Feb 18th, 2015
Recommended Rspec style for Rails models
Feb 11th, 2015