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.
IMHO the solution here is somewhat what Damian Rivas says there, but the important thing is:
in rails helper you have a line:
require 'spec_helper'
in spec_helper you have in config block line to include devise helpers:
config.include Devise::Test::ControllerHelpers, type: :controller
you also as said above have to have rspec/rails require line
The 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 :)
Related
I'm trying to use this line in a minitest test that uses capybara, poltergeist, and phantomjs:
bip_select(#gs, :goal_id, Goal.first.name)
This is a helper that best_in_place offers to simulate a user choosing a value from a field. I've read a few questions elsewhere on StackOverflow where other developers who are using RSpec have added this line to their spec_helper.rb file:
config.include BestInPlace::TestHelpers
I've tried adding this line to my test_helper.rb file and I've tried adding it to the test in question. But I'm still getting the error
NoMethodError: undefined method `bip_select' for #<GoalStudentsPoltergeistEditTest:0x00000006d85148>
Thank you in advance for any insight.
To get the method definition available in your integration tests, edit test_helper.rb to include the lines referring to Best in Place (other lines left for context):
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'best_in_place/test_helpers'
# ...
class ActionDispatch::IntegrationTest
include BestInPlace::TestHelpers
end
I'm using Rails 4.1 and Ruby 2.0.0. I'm trying to set up testing with minitest-rails and I'm running into this strange error. When I include:
require 'minitest/spec'
In my 'spec_helper' file it give me a NameError: uninitialized constant Minitest::VERSION error. When I comment out this line, everything seems to work fine. The odd thing is that 'minitest/autorun' is also in there and not causing any problems. Maybe you guys can shed some light on what's going on here.
spec_helper.rb:
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'minitest/spec'
require 'minitest/autorun'
require 'minitest-rails'
require 'minitest-rails-capybara'
Rakefile:
require File.expand_path('../config/application', __FILE__)
Pinteresting::Application.load_tasks
namespace :test do
task :run do
ENV["RACK_ENV"] = "test"
$LOAD_PATH.unshift("lib", "spec")
if ARGV[1]
require_relative ARGV[1]
else
Dir.glob("./spec/**/*_spec.rb").each { |file| require file }
end
end
end
.spec:
require "spec_helper"
describe "Test" do
describe "When two is equal to two" do
it "asserts true" do
assert_equal(2, 2)
end
end
end
Stack trace:
nbp-93-202:pinteresting Frank$ rake test:run
rake aborted!
NameError: uninitialized constant Minitest::VERSION
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/minitest-5.3.4/lib/minitest/unit.rb:22:in `<class:Unit>'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/minitest-5.3.4/lib/minitest/unit.rb:21:in `<module:Minitest>'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/minitest-5.3.4/lib/minitest/unit.rb:20:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/minitest-5.3.4/lib/minitest/spec.rb:1:in `<top (required)>'
/Users/Frank/Desktop/pinteresting/spec/spec_helper.rb:4:in `<top (required)>'
/Users/Frank/Desktop/pinteresting/spec/diagnostic_spec.rb:1:in `<top (required)>'
/Users/Frank/Desktop/pinteresting/Rakefile:12:in `block (3 levels) in <top (required)>'
/Users/Frank/Desktop/pinteresting/Rakefile:12:in `each'
/Users/Frank/Desktop/pinteresting/Rakefile:12:in `block (2 levels) in <top (required)>'
Tasks: TOP => test:run
Interestingly, if try to run or require a file with just the two requires minitest/spec and minitest/autorun the interpreter raises a warning saying that you should require 'minitest/autorun' instead or add "gem 'minitest'" before require 'minitest/autorun', although it doesn't rise the NameErrorto me.
So switching the require statements around (in order to first require minitest/autorun) seems to do the trick. Requiring minitestin the first place seems to do the trick also.
I think you can resolve this warning by making your implementation simpler. In spec/spec_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/rails"
require "minitest/rails/capybara"
You are missing the require for rails/test_help. Did you remove that for a specific reason?
In Rakefile:
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
Rails.application.load_tasks
Rails::TestTask.new("test:spec" => "test:prepare") do |t|
t.pattern = "spec/**/*_spec.rb"
end
Rake::Task["test:run"].enhance ["test:spec"]
And now run either $ rake test:spec to run all your specs, or $ rake test to run all your tests. The reason behind keeping the rake tasks under the test namespace is because this is what Spring keys on to use the running test environment. Spring uses the task namespace, not the directory name.
I have:
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
which properly work with simple rspec spec/model/user_spec.rb (allows me to use create(:user), not FactoryGirl.create(:user)).
But if I use zeus rspec spec/model/user_spec.rb to speed up my specs, it troughs me an error:
Failure/Error: #user = create(:user)
NoMethodError:
undefined method `create' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007fc8618e4960>
How I can use this syntax with zeus?
Remove either of these lines in spec/spec_helper.rb if they exist:
require 'rspec/autorun'
require 'rspec/autotest'
Did you previously use spork on this project? If so, you have to remove the parts that Spork changed in your spec_helper. Like #ilake-chang said, you have to remove the require 'rspec/autorun' and you'll also want to remove Spork.prefork and Spork.each_run.
See the Zeus wiki on Spork
When running my specs with rspec & capybara, it can't find capybara's visit method. Is there another initialization step I need to do?
$bundle exec rspec spec
/home/brian/projects/expense_track/expense_track/spec/requests/homepage_spec.rb:6:in `block (2 levels) in <top (required)>': undefined method `visit' for #<Class:0xb6572b8> (NoMethodError)
Gemfile:
group :test, :development do
gem "rspec-rails"
gem "capybara"
end
top of my spec_helper.rb:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'rspec/autorun'
homepage_spec.rb:
require 'spec_helper'
describe "The home page" do
context "home page exists" do
visit "/"
page.should have_content("elephants")
end
end
Just ran into this issue myself.
So the reason for this is there has been a somewhat undocumented change in Capybara. Capybara now makes the assumption that anything using it needs to be in the spec/features folder and it will make the proper assumptions. Anything left in the spec/requests folder will no longer work. Though there are workarounds.
For a context block you can add the parameter :type => :feature and this will fix that issue or you can change the name of a describe method at the beginning of a spec to feature and this should change it as well.
They announced this change in their Google group: https://groups.google.com/forum/?fromgroups=#!topic/ruby-capybara/5KfxezI-U0Q
Notably, we changed the :type that Capybara assumes your specs run under in RSpec to :feature (previously it was :request). The latest release of spec/features. Alternatively you can use the Capybara Feature DSL (feature instead of describe), which should work without any additional tweaking. If you see errors like undefined method visit, then you're probably encountering this issue. If you're including modules into :request specs, you will probably need to change that to :feature.
This was further discussed in the github issue: https://github.com/jnicklas/capybara/issues/814
A few things to note here :
The changes in Capybara 2.0.x are documented here https://github.com/rspec/rspec-rails/blob/master/Capybara.md . There are changes in the spec directory structure : spec/features, spec/controllers, spec/views, spec/helpers, spec/mailers.
load Capybara dsl explicitly inside your spec_helper
require 'capybara/rails'
require 'capybara/rspec'
include Capybara::DSL
This worked for me.
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/rails'
RSpec.configure do |config|
config.include Capybara::DSL, :type => :request
end
This enables you to use Capybara's helpers inside spec/requests.
Because RSpec.configure not including capybara DSL in spec_helper.rb
It is an ugly solution, but you can add this to your spec_helper.rb.
module ::RSpec::Core
class ExampleGroup
include Capybara::DSL
include Capybara::RSpecMatchers
end
end
The git issue for this:
https://github.com/rspec/rspec-rails/issues/503
Unfortunately this workaround doesn't do it for me. I still get
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x007fbfeb535298>
The repo is public under: https://github.com/ikusei/Goldencobra_Newsletter
You need to look at the branch '28817499-subscribe'
edit: If i put include Capybara::DSL inside my describe block it works.
but including Capybara::DSL in the global scope is not recommended!
Because I do not know a good way.
For rspec 3 and rails, make sure you are using require "rails_helper", instead of require "spec_helper".
Otherwise, review the latest changes to rspec 3 & rspec-rails and Capybara 2.0.x.
I'm setting up a Rails 3.1 project and like to properly test it using RSpec.
After running rails g rspec:install and further running rspec, the console messages read as follows:
% rspec
/Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/configuration.rb:470:in `assert_no_example_groups_defined': RSpec's mock_framework configuration option must be configured before any example groups are defined, but you have already defined a group. (RSpec::Core::Configuration::MustBeConfiguredBeforeExampleGroupsError)
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/configuration.rb:168:in `mock_framework='
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/configuration.rb:142:in `mock_with'
from /Users/ened/project/spec/controllers/../spec_helper.rb:19
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core.rb:71:in `configure'
from /Users/ened/project/spec/controllers/../spec_helper.rb:11
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in `require'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in `require'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:225:in `load_dependency'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in `require'
from /Users/ened/project/spec/controllers/submissions_controller_spec.rb:1
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:234:in `load'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:234:in `load'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:225:in `load_dependency'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:234:in `load'
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/configuration.rb:459:in `load_spec_files'
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/configuration.rb:459:in `map'
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/configuration.rb:459:in `load_spec_files'
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/command_line.rb:18:in `run'
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/runner.rb:80:in `run_in_process'
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/runner.rb:69:in `run'
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/runner.rb:10:in `autorun'
from /usr/bin/rspec:19
My rspec/spec_helper.rb looks like this:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
end
I thought it's configured already using the config.mock_with ? I'm puzzled, what is missing?
I just hit this. It turned out to be that some of my old (circa RSpec 1) specs had the following require statement:
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
Most of the new specs had this require statement:
require 'spec_helper'
I'd never bothered to tidy up the old style. What this meant was that the spec_helper.rb filename was passed to require in two different ways: one full path, one local path. This in turn caused spec_helper.rb to be executed twice, triggering the error. Changing all the require statements to the short new style fixed the issue.
I just resolved this problem for my Rails app.
My problem was that two spec files were missing the require 'spec_helper' line at the top of the file.
I met the same problem, and my root cause is:
there are some spec files exist in spec/support folder!
e.g.
spec/support/xx_spec.rb
which looks like:
require 'spec_helper'
describe XX do
...
and in the spec/spec_helper.rb file, there are:
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
This is a situation that 'spec_helper' was recursive required for unlimited times.
So the solution is very simple:
REMOVE all the xx_spec.rb files from spec/support folder.
Could be an issue of Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}. I have placed it below the config block in my app and it worked.