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)
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
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.
Related
I am trying to include a custom module into an RSpec test. However, I keep getting errors telling me it can't find my module, it says NameError: uninitialized constant AuthHelper.
I have been trying to follow this https://stackoverflow.com/a/68669816/5568244
I didn't know where to put the the module so I created a folder called specs/support/ and a file called auth_helper.rb in it and put module AuthHelper ...etc... end in that
I have tried simply including it in the test
spec/requests/topics_specs.rb
require 'rails_helper'
include AuthHelper
which i expected to be able to just automatically find the module in the support folder but it didn't. Should it? Does RSpec look in specific places?
I also tried to set up the rails_helper.rb file to include it
RSpec.configure do |config|
config.include(AuthHelper, :type => :request)
end
This still didn't work.
What am i missing? How should this work?
I'm working on my first app since I installed Rails 5. When I ran my specs for controller actions, I got the warning message below even though all my tests were passing.
[Devise] including `Devise::TestHelpers` is deprecated and will be removed from Devise.
For controller tests, please include `Devise::Test::ControllerHelpers` instead.
So in spec/rails_helper.rb I change this line:
config.include Devise::TestHelpers, type: :controller
to
config.include Devise::Test::ControllerHelpers
This change made the warning go away, but now the specs for models are failing. (They were passing before the change.) How should I fix this? Thanks!
You should change your spec/rails_helper.rb file to the following:
config.include Devise::Test::ControllerHelpers, type: :controller
This will ensure that the Devise::Test::ControllerHelpers module is only be included in your controller tests. The reason your model tests are failing is because that module is specific to controller tests.
I am getting a strange issue when using Guard to run my specs.
I am running a feature spec that uses the Capybara "feature" / "scenario" syntax. I am also using Spring.
Everything works fine if I run rspec spec or spring rspec in the console or rspec in the Guard shell. But, when the watched specs get run automatically by Guard, I get the following error:
/spec/features/navigation_spec.rb:1:in <top (required)>': undefined methodfeature' for main:Object (NoMethodError)
Why is it not picking up the Capybara syntax only in this specific context?
Here is the relevant code:
GuardFile
guard :rspec, :spring => true do
watch(%r{^spec/.+_spec\.rb$})
end
spec/features/navigation_spec.rb
feature "navigation" do
context "When on the home page" do
before { visit "/" }
scenario "I should see the navigation header" do
expect(page).to have_selector("div.navigation")
end
end
end
spec/spec_helper.rb
require 'capybara/rspec'
For anyone who may run into a similar issue in the future, I forgot to include require 'spec_helper' in the feature spec (like an idiot).
In Rails 4, make sure that you have included 'rails_helper' instead of 'spec_helper' on top of your specfile:
require 'rails_helper'
feature "Some Feature", :type => :feature do
..
end
And also make sure that config.disable_monkey_patching! is commented out or removed. Otherwise you will encounter problems when running your feature specs.
require 'capybara/rspec'
RSpec.configure do |config|
..
# config.disable_monkey_patching!
..
end
If you have created a .rspec file inside your project dir, also make sure to to change spec_helper to rails_helper there as well.
How are you invoking guard? It sounds like you might need to do bundle exec guard to kick things off. It could also be running under the wrong environment (unlikely, but worth a look).
Right now I rspec configured so that running the rspec command by itself excludes any tests that require loading the Rails environment:
RSpec.configure do |config|
config.filter_run_excluding :type => 'feature'
end
The excluded tests look like the following:
describe 'Feature that requires rails', :type => :feature do
# test, test, test
end
The command rspec -t type:feature will run these tests exclusively.
With this configuration, is there a way to run all tests in one command, including the feature tests?
The way I accomplish this is to use an environment variable so changing what you have to:
RSpec.configure do |config|
config.filter_run_excluding :type => 'feature' unless ENV["ALLOW_FEATURES"]
end
and then running your tests with:
ALLOW_FEATURES=true rspec
will ignore the exclude and run all tests
When I visit my sign in page in a browser everything works fine.
When I visit my sign in page in an rspec integration/request test, I get the following error:
ActionView::Template::Error:
undefined method `title' for #<#<Class:0x00000007af9180>:0x00000007af32a8>
The title method is used by the view and defined in ApplicationHelper which devise seems to find when using the browser. However, during rspec integration tests, devise is unable to find the helper method.
Is there anything I should be stubbing? It seems wrong to be stubbing in integration tests. Any other ideas?
(This question is not about how to include devise helpers in integration tests. I'm manually filling in the sign in forms to authenticate).
Looks like this issue. (in some cases related to ActiveAdmin https://github.com/gregbell/active_admin/wiki/Use-spork)
Here I found a hack that works for me (REE 1.8.7, Rails 3.1, Capybara, Devise, active_admin).
However, this is not likely to be merged, so I forked spork-rails to here with that patch applied. And as you probably know I can point my Gemfile to that repo:
gem 'spork-rails', :git => "git://github.com/chopmo/spork-rails.git"
Not optimal but it gets the job done for now.
I had a similar problem using Cucumber when I installed devise:
undefined local variable or method `flash_block' for #<#<Class:0x007ffd0a28dae8>:0x007ffd0b2f6d58> (ActionView::Template::Error)
I solved it by including the module in env.rb
Spork.prefork do
include FlashBlockHelper
I hope this helps.
Inside /spec/support create devise.rb with this:
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
Make sure your spec_helper.rb includes:
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
and that your specs have:
require 'spec_helper'