Gordon
2011-Oct-14 04:40 UTC
[rspec-users] How do we include support helpers in request specs?
Hi, all :) I have some working support helpers defined in spec/support/ controller_macros.rb. I''m running "rake spec" before a ''git commit'' and observed that the spec request for my ''brands'' controller, spec/requests/brands_spec.rb fails because a support helper method I defined in spec/support/controller_macros.rb is not recognised. I got the following error: ----------------- Error extract: start --------------------------- spec/requests/brands_spec.rb:4:in `block in <top (required)>'': undefined local variable or method `login_admin_user'' for #<Class: 0x00000103bdf9a8> (NameError) from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ rspec/core/example_group.rb:142:in `module_eval'' from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ rspec/core/example_group.rb:142:in `subclass'' from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ rspec/core/example_group.rb:129:in `describe'' from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ rspec/core/dsl.rb:5:in `describe'' from /Users/anexiole/projects/try_rails/spec/requests/brands_spec.rb: 3:in `<top (required)>'' from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/ active_support/dependencies.rb:235:in `load'' from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/ active_support/dependencies.rb:235:in `block in load'' from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/ active_support/dependencies.rb:227:in `load_dependency'' from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/ active_support/dependencies.rb:235:in `load'' from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ rspec/core/configuration.rb:419:in `block in load_spec_files'' from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ rspec/core/configuration.rb:419:in `map'' from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ rspec/core/configuration.rb:419:in `load_spec_files'' from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ rspec/core/command_line.rb:18:in `run'' from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ rspec/core/runner.rb:80:in `run_in_process'' from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ rspec/core/runner.rb:69:in `run'' from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ rspec/core/runner.rb:11:in `block in autorun'' rake aborted! ----------------- Error extract: end --------------------------- I noticed that in the stack, the helper file, spec/support/ controller_macros.rb was never called. My model, view and controller specs all pass. This is what my spec/spec_helper.rb file looks like: ---------- spec/spec_helper.rb start ------------------- ENV["RAILS_ENV"] ||= ''test'' require File.expand_path("../../config/environment", __FILE__) require ''rspec/rails'' require ''webrat'' # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} RSpec.configure do |config| # == Mock Framework # # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: # # config.mock_with :mocha # config.mock_with :flexmock # config.mock_with :rr #: config.mock_with :rspec config.include Devise::TestHelpers, :type => :controller config.extend ControllerMacros, :type => :controller config.before(:suite) do DatabaseCleaner.strategy = :transaction DatabaseCleaner.clean_with(:truncation) end config.before(:each) do DatabaseCleaner.start end config.after(:each) do DatabaseCleaner.clean end # # Rspec 2-https://github.com/plataformatec/devise # config.include Devise::TestHelpers, :type => :controller # Remove this line if you''re not using ActiveRecord or ActiveRecord fixtures # config.fixture_path = "#{::Rails.root}/spec/fixtures" # If you''re not using ActiveRecord, or you''d prefer not to run each of your # examples within a transaction, remove the following line or assign false # instead of true. # config.use_transactional_fixtures = true end ---------- spec/spec_helper.rb end ------------------- This is what the failing request spec, spec/request/brands_spec.rb looks like. As this resource will not be available to non-admin users, it will redirect non-admin users to the root url. To have this spec passing, I am calling the method, login_admin_user which uses factory girl to create an admin user object (and sign in). This works without any problems in the controller specs, spec/controller, brands_controller_spec.rb. Strangely, it fails to work here regardless of where I put the call to login_admin_user right after ''describe "Brands" do'' or '' describe "GET /brands" do''. ------------ spec/request/brands_spec.rb start ------------------ require ''spec_helper'' describe "Brands" do login_admin_user describe "GET /brands" do it "works! (now write some real specs)" do get brands_path response.status.should be(200) end end end ------------ spec/request/brands_spec.rb end ------------------ Can someone please show me how do I ensure that helper methods defined in spec/support get called in spec/request specs? thank you :)
Justin Ko
2011-Oct-14 04:49 UTC
[rspec-users] How do we include support helpers in request specs?
On Oct 13, 2011, at 10:40 PM, Gordon wrote:> Hi, all :) > > I have some working support helpers defined in spec/support/ > controller_macros.rb. > > I''m running "rake spec" before a ''git commit'' and observed that the > spec request for my ''brands'' controller, > spec/requests/brands_spec.rb fails because a support helper method I > defined in spec/support/controller_macros.rb is not > recognised. > > I got the following error: > > ----------------- Error extract: start --------------------------- > > spec/requests/brands_spec.rb:4:in `block in <top (required)>'': > undefined local variable or method `login_admin_user'' for #<Class: > 0x00000103bdf9a8> (NameError) > from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ > rspec/core/example_group.rb:142:in `module_eval'' > from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ > rspec/core/example_group.rb:142:in `subclass'' > from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ > rspec/core/example_group.rb:129:in `describe'' > from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ > rspec/core/dsl.rb:5:in `describe'' > from /Users/anexiole/projects/try_rails/spec/requests/brands_spec.rb: > 3:in `<top (required)>'' > from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/ > active_support/dependencies.rb:235:in `load'' > from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/ > active_support/dependencies.rb:235:in `block in load'' > from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/ > active_support/dependencies.rb:227:in `load_dependency'' > from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/ > active_support/dependencies.rb:235:in `load'' > from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ > rspec/core/configuration.rb:419:in `block in load_spec_files'' > from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ > rspec/core/configuration.rb:419:in `map'' > from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ > rspec/core/configuration.rb:419:in `load_spec_files'' > from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ > rspec/core/command_line.rb:18:in `run'' > from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ > rspec/core/runner.rb:80:in `run_in_process'' > from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ > rspec/core/runner.rb:69:in `run'' > from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/ > rspec/core/runner.rb:11:in `block in autorun'' > rake aborted! > > > ----------------- Error extract: end --------------------------- > > > I noticed that in the stack, the helper file, spec/support/ > controller_macros.rb was never called. > My model, view and controller specs all pass. > > This is what my spec/spec_helper.rb file looks like: > > ---------- spec/spec_helper.rb start ------------------- > > ENV["RAILS_ENV"] ||= ''test'' > require File.expand_path("../../config/environment", __FILE__) > require ''rspec/rails'' > require ''webrat'' > > # Requires supporting ruby files with custom matchers and macros, etc, > # in spec/support/ and its subdirectories. > Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} > > RSpec.configure do |config| > # == Mock Framework > # > # If you prefer to use mocha, flexmock or RR, uncomment the > appropriate line: > # > # config.mock_with :mocha > # config.mock_with :flexmock > # config.mock_with :rr > #: config.mock_with :rspec > config.include Devise::TestHelpers, :type => :controller > config.extend ControllerMacros, :type => :controller:type => :controller will only include it in controller specs. If you want to include them in requests, you need to add this: config.extend ControllerMacros, :type => :requests> > config.before(:suite) do > DatabaseCleaner.strategy = :transaction > DatabaseCleaner.clean_with(:truncation) > end > > config.before(:each) do > DatabaseCleaner.start > end > > config.after(:each) do > DatabaseCleaner.clean > end > > # # Rspec 2-https://github.com/plataformatec/devise > # config.include Devise::TestHelpers, :type => :controller > > # Remove this line if you''re not using ActiveRecord or ActiveRecord > fixtures > # config.fixture_path = "#{::Rails.root}/spec/fixtures" > > # If you''re not using ActiveRecord, or you''d prefer not to run each > of your > # examples within a transaction, remove the following line or assign > false > # instead of true. > # config.use_transactional_fixtures = true > > end > > ---------- spec/spec_helper.rb end ------------------- > > > This is what the failing request spec, spec/request/brands_spec.rb > looks like. As this resource will not be available to non-admin users, > it will redirect non-admin users to the root url. To have this spec > passing, I am calling the method, login_admin_user which uses factory > girl to create an admin user object (and sign in). This works without > any problems in the controller specs, spec/controller, > brands_controller_spec.rb. Strangely, it fails to work here regardless > of where I put the call to login_admin_user right after ''describe > "Brands" do'' or '' describe "GET /brands" do''. > > ------------ spec/request/brands_spec.rb start ------------------ > require ''spec_helper'' > > describe "Brands" do > login_admin_user > > describe "GET /brands" do > it "works! (now write some real specs)" do > get brands_path > response.status.should be(200) > end > end > end > > ------------ spec/request/brands_spec.rb end ------------------ > > > Can someone please show me how do I ensure that helper methods defined > in spec/support get called in spec/request specs? > > thank you :) > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users