I have a method in my application helper but for some reason it isn't being found when I run my rspec tests. Here's the error:
Failure/Error: helper.chart_series(user).should =~ /y: 100/
undefined method `ab_variant' for #<ActionView::Base:0x102b12d78>
# ./app/helpers/sidebar_helper.rb:17:in `chart_series'
# ./spec/helpers/sidebar_helper_spec.rb:35
How do i make sure ab_variant (within application helper) can be found by my rspec tests?
Your helpers defined in app/helpers aren't loaded in rspec by default. You have to re-create them.
So create a file and call it a name you like, for ex. "application_helper.rb" and place it inside spec/support. This will be included automatically and will be available in your specs by this file in spec/spec_helper.rb:
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
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 have some common code that I have abstracted to a module in my /lib directory:
module ExampleModule
def self.do_something
# return a value
end
end
I need to use this method in my test fixtures, however I am getting the following error when I run tests:
Error:
SomeTest#test_that_something:
NoMethodError: undefined method `do_something' for ExampleModule:Module
test/fixtures/model_name.yml:4:in `get_binding'
How can I access this method in the test fixtures? I can fix the error by requiring the module in other parts of the app that are loaded during test, but how would I require this module if it was needed specifically in the fixture file here? Would I need to require it in any test using the fixture?
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 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'