Search code examples
ruby-on-railsruby-on-rails-3rspecrspec2rspec-rails

RSpec not loading routes when namespacing directories


For easier organization, I'd like to namespace a couple folders under my /spec directory. So rather than /spec/requests, I'd like to use /spec/another_directory/requests.

However, when I run my specs under these new namespaced directories, I get

NoMethodError:
       undefined method `whatever_path'

So, it looks as those my routes are no longer being properly loaded. If I move the file back up to spec/requests (without the namespace), all is well and the test is green.

Not really sure what the issue is. I'm requiriing 'spec_helper' in my files. I've also seen:

require File.dirname(__FILE__) + '/../../spec_helper'

and its variations, but I'm not really sure how that helps because it seems to load the spec_helper, just not the routes. And to further make things fun, I'm reloading the routes before each run in the Spork.pre_fork block

Spork.each_run do
  load "#{Rails.root}/config/routes.rb"
end

but I still get the error whether Spork is running or not.

What am I doing wrong?

(rspec-rails 2.8.1)


Solution

  • I think, this is due request example before hook is not fired. If you look further, you can understand how rspec-rails decide which example type it runs.

    I suggest you to use spec/requests/another_directory schema or you can try to reconfigure Rspec somewhat like:

    RSpec::configure do |config|
       config.include RSpec::Rails::RequestExampleGroup, :type => :request,
            :example_group => {:file_path => 'path/to/your/requests')}
    end