Common controller action to run from a single file using Rspec

36 views
Skip to first unread message

Arup Rakshit

unread,
Dec 15, 2015, 7:19:55 AM12/15/15
to rspec
Hi,

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 ?

Jon Rowe

unread,
Dec 15, 2015, 7:37:13 PM12/15/15
to rs...@googlegroups.com
Try requiring the files manually, Rails autoloading is sometimes a bit funny.

Jon Rowe
---------------------------

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/a5f0915a-870e-4595-a600-45134bbe9546%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Arup Rakshit

unread,
Dec 20, 2015, 12:05:49 PM12/20/15
to rspec
Hello Jon,

Reboot fixed it. :) 
Reply all
Reply to author
Forward
0 new messages