How to dry control tests

I have dozens of controller tests that go to the tune:

test "GET #show when not authenticated" do
  get :show, { id: 1 }
  assert_redirected_to '/login'
end

But mine application_controlleris locked by default.

class ApplicationController < ActionController::Base
  before_action :ensure_logged_in
end

I do not need to add this test to each controller test. But I do not want to check only one controller.

How can I verify that my application is blocked without clogging my tests with this repetition?

+4
source share
1 answer

You can use:

test "Should redirect to login when user is not logged in"  do
    [:show, :edit, :new].each do |action|
      get action, {id: 1}
      assert_redirected_to '/login'
    end
end

And name this test as follows:

test 'Redirected to the login page if not authorized' do

+4
source

Source: https://habr.com/ru/post/1608686/


All Articles