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
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?
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.
In the Factory Girl getting started documentation it says to include the helper module like this:
# RSpec
# spec/support/factory_girl.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
Inside the RSpec config block. But this doesn't work for me. I just put module inclusion the beginning of the rails_helper.rb and it works fine.
My question is why does the documentation say to put it in the config block? Should that be working? Am I going to run into method collisions. Am I going against best practices?
Files residing in spec/support are not loaded by default. Uncomment line #21 in spec/rails_helper.rb for Rails 4.2.
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.
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'