RSpec not loading routes when namespacing directories
-
28-10-2019 - |
Question
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
OTHER TIPS
I ended up doing:
config.include RSpec::Rails::RequestExampleGroup, :type => :request,
:example_group => {
:file_path => config.escaped_path(%w[spec (my|folder|names) requests])}
But, then it's not getting the Capybara methods, so I include then again after the above config block
config.include Capybara::DSL, :type => :request
config.include Capybara::RSpecMatchers, :type => :request
I wanted to make the path more elastic so I don't have to have include each 'parent' folder. I tried using ** instead of having to include (each|folder), but it wasn't loading. I then tried setting the path directly using:
:file_path => "spec/../requests"
but then I lose the ability to run a single file.
Either way, the above code works, just have to pipe each of your folder's names.