Skip to content

Cop Idea: Ban @controller = ... in controller specs #73

@ndbroadbent

Description

@ndbroadbent

Is your feature request related to a problem? Please describe.

We just ran into an issue where I was doing this for an ApplicationController spec to test some helper methods:

class TestApplicationController < ApplicationController
  def test
    head :ok
  end
end

RSpec.describe ApplicationController do
  draw_test_routes! { get '/test', to: 'test_application#test' }

  before(:each) do
    @controller = TestApplicationController.new

It turns out that this is a huge footgun, and caused an order-dependent test failure. By setting @controller, RSpec is no longer able to clean up after the test, and it breaks view rendering (even for feature specs!)

We spent a very long time trying to figure out why some tests were suddenly failing on CI after a Rails/RSpec upgrade.

Describe the solution you'd like

A new rule that bans any instance of @controller = in spec/controllers

Additional context

This article describes the issue in great detail: https://www.vector-logic.com/blog/posts/what-the-spec-order-dependent-test-failure

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions