Reputation: 319
Searching for this problem has only turned up issues with specific gems or classes that other people have dealt with. In my case, I think there's something wrong with rspec in general.
Whenever I generate a controler with rails generate ControllerName
, it sets everything up and things seem to work fine. I've routed a few controllers and tested them in development and production and everything works.
The only thing that seems to be broken is testing them with rspec. Right now I have two controllers in my project, and whenever I run rspec/spec, most specs generated by rails g
turns up this error:
NoMethodError:
undefined method `setup' for RSpec::ExampleGroups::MoreStuffHere
For example, I have a WelcomeController and ApplicationsController and these are the errors I keep getting:
undefined method `setup' for RSpec::ExampleGroups::ApplicationsController:Class
undefined method `setup' for RSpec::ExampleGroups::WelcomeController:Class
undefined method `setup' for RSpec::ExampleGroups::WelcomeAboutHtmlErb:Class
undefined method `setup' for RSpec::ExampleGroups::WelcomeIndexHtmlErb:Class
Interestingly, I don't get errors for their helper_spec
's
Here is a full error in case this helps:
An error occurred while loading ./spec/controllers/applications_controller_spec.rb.
Failure/Error:
RSpec.describe ApplicationsController, type: :controller do
end
NoMethodError:
undefined method `setup' for RSpec::ExampleGroups::ApplicationsController:Class
# ./spec/controllers/applications_controller_spec.rb:3:in `<top (required)>'
Does anyone have any idea where this issue could lie?
Upvotes: 6
Views: 4072
Reputation: 466
IMHO the solution here is somewhat what Damian Rivas says there, but the important thing is:
require 'spec_helper'
config.include Devise::Test::ControllerHelpers, type: :controller
rspec/rails
require lineThe main point there was that you have to watch the order here... always including rspec/rails
has to go before spec_helper
So lets say example spec_helper.rb
looks like:
require 'rubygems'
Rspec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
end
Then our rails_hepler.rb
has to look like:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'rspec/rails'
require 'spec_helper'
Then in sample_spec.rb
you require rails_helper
, or in my case I require through .rspec
so I don't have to have it on top of every spec.
Hope it helps somebody :)
Upvotes: 2
Reputation: 319
In spec_helper.rb
I had
require File.expand_path("../../config/environment", __FILE__)
require 'rails/all'
RSpec.configure do |config|
# More code here.
end
All I had to do was add require 'rspec/rails'
under require 'rails/all'
. This solved my problem.
Yet, I don't know why. If someone can elaborate that would be great. I already had require 'rspec/rails'
in rails_herlper.rb
, but obviously that wasn't good enough.
Upvotes: 3