Is there a way to include the config_for settings in rspec tests? I have the following settings configured in config/initialisers/settings.rb:
SETTINGS = Rails.application.config_for(:settings)
but they aren't autoloaded in rspec and it triggers the following:
Failure/Error: generator = SETTINGS['my_strategy'].constantize.new(file)
NoMethodError:
undefined method `constantize' for nil:NilClass
As it isn't a module or class, I can't include it in the normal way and doing something as:
config.include Rails.application, type: :controller
Is in my opinion, not the proper thing to do.
After some digging I couldn't find a rspec helper which can load the config_for settings. I solved this by doing the following:
before :all do
SETTINGS['my_strategy'] = 'StrategyClass'
end
Which solved the loading of the constant.
Related
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 trying to use the Spree 2.3 route helpers in Rspec 3.0. In the main app, I can access them by prefixing spree., like so:
spree.admin_login_path => 'spree/admin/user_sessions#new'
However I can't seem to access them in Rspec.
#rspec error
undefined local variable or method `spree_admin_login_path'
I've found reference to including the helpers in the rails_helper file, but this throws an error
# app/spec/rails_helper.rb
RSpec.configure do |config|
config.include Spree::Core::UrlHelpers
end
# configuring the above results in the following
app/spec/rails_helper.rb:21:in `block in <top (required)>': uninitialized constant Spree (NameError)
How do I access the spree routes given in $ rake routes in my tests?
After digging through the Spree code I was able to put together this setup for my rails_helper file that lets me use spree routes such as spree.admin_login_path in my spec files:
# app/spec/rails_helper.rb
require 'spree/testing_support/url_helpers'
RSpec.configure do |config|
config.include Spree::TestingSupport::UrlHelpers
end
I'm sure there's a smoother way to include all of Spree's test helpers, and I'd love to hear about it from someone who knows.
I'm trying to build some tests with Rspec and I'm getting the following error:
Failure/Error: get :index
NoMethodError:
undefined method `authenticate!' for nil:NilClass
Then I made some searchs and I realized that I had to include this line in the spec_helper.rb:
RSpec.configure do |config|
config.include Devise::TestHelpers, type: :controller
end
It seems that this code worked for most people but not for me. Now i'm getting the following error:
/home/bruna/Dropbox/Rails/academico/spec/spec_helper.rb:18:in `block in <top (required)>': uninitialized constant Devise (NameError)
I think this might be some error in this version of rspec and/or devise. Has anyone seen this error? Please, help me :'(
RSpec 3 configuration is different from earlier versions, with the addition of a spec/rails_helper.rb file that must be included for each spec file. Add the Devise TestHelpers to the spec/rails_helper.rb file.
If you need more information about RSpec 3 configuration, I've written an RSpec Tutorial. There's also information about testing Devise with RSpec, including advice about session helpers for feature testing, in my Rails Devise Tutorial.
I am using Rails 3.2, RailsAdmin 0.0.3, Rspec and Capybara.
I try to call RailsAdmin url helpers from my spec file as it is explained in the RailsAdmin wiki. rails_admin.new_path(:model_name => :user)
When I use the helper that way from a controller or a view it works nice but when trying to use it from a spec file it gives this error:
undefined local variable or method `rails_admin' for #RSpec::Core::ExampleGroup::Nested_2:0xbe04948>
I guess that I have to add something to my spec_helper.rb file in order to load rails_admin. But after googling for a while and looking to rails_admin gem's spec_helper, I cannot figure out what...
Any help will be apreciated!
I found it in spec_helper.rb of rails_admin.
Include the following code into spec_helper.rb.
RSpec.configure do |config|
...
config.include RailsAdmin::Engine.routes.url_helpers
end
I tested using debugger. It can be called
> new_path(:model_name => :user)
=> "/admin/user/new"
I added this line to individual specs that needed rails_admin urls and it allowed me to reference the methods mentioned in a prior post, but without interfering with my non-admin urls.
include RailsAdmin::Engine.routes.url_helpers
Example of method within spec
index_path(:model_name => :client)
I am using Rails 5.0, Rspec 3.5 and Capybara 2.10.1
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'