5

I'm having an inexplicably hard time getting my named routes to work within my rspec tests.

Here's what I'm seeing:

1) Home GET / works!
 Failure/Error: get root_path
 NameError:
   undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fe13b0c1538>
 # ./spec/requests/home_spec.rb:6:in `block (3 levels) in <top (required)>'

2) Home GET / shows products!
 Failure/Error: get products_path
 NameError:
   undefined local variable or method `products_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fe13b0cdea0>
 # ./spec/requests/home_spec.rb:10:in `block (3 levels) in <top (required)>'

spec/requests/home_spec.rb

require 'spec_helper'

describe "Home" do
  describe "GET /" do
    it "works!" do
      get root_path
      response.status.should be(200)
    end
    it "shows products!" do
      get products_path
      response.status.should be(200)
    end
  end  
end

spec/spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

# 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}

require 'spree/core/testing_support/factories'

RSpec.configure do |config|
  config.mock_with :rspec
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
  config.before(:suite) do
    # DatabaseCleaner.strategy = :truncation
  end
  config.before(:each) do
    # DatabaseCleaner.start
  end
  config.after(:each) do
    # DatabaseCleaner.clean
  end

  config.include Rails.application.routes.url_helpers
end

You can see that the test is including spec_helper, and spec_helper is including the routing methods. Here's my rake routes:

bundle exec rake routes

...
             root        /                        spree/home#index
         products GET    /products(.:format)      spree/products#index
...

Any idea what could be going on here to prevent the routes from working? Thanks!

isthmuses
  • 1,316
  • 1
  • 17
  • 27
  • Are you using spork? And did you restart it after you included url_helpers in your spec_helper? – usha Sep 30 '13 at 19:22
  • Good thought, but I am not using spork, so I don't think it's a restart issue! – isthmuses Sep 30 '13 at 19:50
  • what about using `visit` method instead of `get`. Regarding to this issue - http://stackoverflow.com/questions/11090038/rspec-and-capybara-difference-between-visit-and-get-methods-with-regards-to-th you should use visit in integration tests – Tom Hert Sep 30 '13 at 20:58
  • @TomHert, Sadly I've tried it with `capybara` and `visit` -- same message about the routes. – isthmuses Sep 30 '13 at 21:28
  • try include this code: `include Rails.application.routes.url_helpers` and put it under this line: `describe "Home" do` – Tom Hert Sep 30 '13 at 22:24

1 Answers1

7

If you're wanting to reference the route of an engine from tests within an dummy application, or just an application itself, then you'll need to prefix the routing helpers with the engine name. For Spree, you would reference the routes like:

spree.products_path

Similarly, if you want to reference routes of the main application from an engine, you'll need to use main_app:

main_app.root_path
Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
  • How does one determine which engine (and therefore namespace) is being used? I'm using whatever is default to Rails and am not having luck finding the engine object – Jeff Mar 24 '14 at 22:39
  • It depends on the controller that is handling the request. – Ryan Bigg Mar 25 '14 at 01:44