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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With