I have very similar index action test pattern in 100+ controllers. That is why I am thinking to run them from a single file. Here is my code to meet the same need :
require 'spec_helper'
Dir[Rails.root.join('app','admin', '**', '*')].each do | file_name |
admin_controller = File.basename(file_name, '.rb')
RSpec.describe "Admin::#{ admin_controller.classify }sController".constantize, type: :controller do
render_views
let(:login_with) { create(:full_admin_user) }
login_admin_user
include_examples "index"
end
end
But, once I run it, getting below error :
/Users/arup/.rvm/gems/ruby-2.2.3@admin/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:263:in `const_get': uninitialized constant Admin::AccountingsController (NameError)
from /Users/arup/.rvm/gems/ruby-2.2.3@admin/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:263:in `block in constantize'
from /Users/arup/.rvm/gems/ruby-2.2.3@admin/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `each'
from /Users/arup/.rvm/gems/ruby-2.2.3@admin/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `inject'
from /Users/arup/.rvm/gems/ruby-2.2.3@admin/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `constantize'
from /Users/arup/.rvm/gems/ruby-2.2.3@admin/gems/activesupport-4.2.5/lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
from /Users/arup/eligible/admin/spec/controllers/controller_index_action_spec.rb:7:in `block in <top (required)>'
from /Users/arup/eligible/admin/spec/controllers/controller_index_action_spec.rb:4:in `each'
from /Users/arup/eligible/admin/spec/controllers/controller_index_action_spec.rb:4:in `<top (required)>'
Those test are for ActiveAdmin controllers. When I run the same test as below :
require 'spec_helper'
RSpec.describe Admin::AccountingsController, type: :controller do
render_views
let(:full_admin_user) { create(:full_admin_user) }
let(:login_with) { full_admin_user }
login_admin_user
include_examples "index"
end
It works like a charm. Now my question is why the code given at top of this thread didn't work ?