I'm trying to use simplecov to check code coverage by tests in Rails application. So I bumped into common problem: the uncovered files are not shown in the report. Adding Rails.application.eager_load! to test_helper.rb as well as changing config/environments/test.rb (solutions for this question: Simple cov gem missing untested files in Rails) didn't work for me.
I tried the solution proposed here: https://github.com/colszowka/simplecov/issues/16#issuecomment-31076575
Now my test_helper.rb looks like this:
require 'simplecov'
SimpleCov.start do
add_group 'Controllers', 'app/controllers'
add_group 'Models', 'app/models'
add_group 'Helpers', 'app/helpers'
add_group 'Mailers', 'app/mailers'
add_group 'Views', 'app/views'
end
all_files = Dir['**/*.rb']
base_result = {}
all_files.each do |file|
absolute = File::expand_path(file)
lines = File.readlines(absolute, :encoding => 'UTF-8')
base_result[absolute] = lines.map do |l|
l.strip!
l.empty? || l =~ /^end$/ || l[0] == '#' ? nil : 0
end
end
SimpleCov.at_exit do
merged = SimpleCov::Result.new(Coverage.result).original_result.merge_resultset(base_result)
result = SimpleCov::Result.new(merged)
result.format!
end
#Rails.application.eager_load!
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
Now after calling rails test I get the error:
Running via Spring preloader in process 17211
Run options: --seed 8726
# Running:
...................................
Finished in 3.251970s, 10.7627 runs/s, 15.6828 assertions/s.
35 runs, 51 assertions, 0 failures, 0 errors, 0 skips
/var/lib/gems/2.3.0/gems/simplecov-0.12.0/lib/simplecov/merge_helpers.rb:29:in `extend_object': can't modify frozen object (RuntimeError)
from /var/lib/gems/2.3.0/gems/simplecov-0.12.0/lib/simplecov/merge_helpers.rb:29:in `extend'
from /var/lib/gems/2.3.0/gems/simplecov-0.12.0/lib/simplecov/merge_helpers.rb:29:in `block in merge_resultset'
from /var/lib/gems/2.3.0/gems/simplecov-0.12.0/lib/simplecov/merge_helpers.rb:28:in `each_key'
from /var/lib/gems/2.3.0/gems/simplecov-0.12.0/lib/simplecov/merge_helpers.rb:28:in `merge_resultset'
from /home/tamila/Ruby/workspace/books_app/test/test_helper.rb:24:in `block in <top (required)>'
from /var/lib/gems/2.3.0/gems/simplecov-0.12.0/lib/simplecov/defaults.rb:67:in `block in <top (required)>'
from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in `fork'
from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in `serve'
from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:131:in `block in run'
from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `loop'
from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `run'
from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in `<top (required)>'
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
Is there a way either to fix this error or to load the files without this bunch of code?
Finally solved it myself by changing the line
merged = SimpleCov::Result.new(Coverage.result).original_result.merge_resultset(base_result)
to
merged = SimpleCov.result.original_result.merge_resultset(base_result)
Still have no idea why it fixed the error.
Related
I have a Rails 5.0.1 app that is NOT backed by any database. When attempting to write a controller test I get a ActiveRecord::ConnectionNotEstablished exception even though I have explicitly configured my spec/rails_helper.rb not to use ActiveRecord and removed references to active_record in config/application.rb. RSpec is trying to run such test within a transaction which requires a database connection to be established. I am attaching the stack trace for reference and I used this Github issue to setup RSpec without ActiveRecord rspec-rails for a rails project without db connection.
Failures:
1) ApplicationController the truth false not equal true
Failure/Error: raise ConnectionNotEstablished, "No connection pool with id #{spec_name} found." unless pool
ActiveRecord::ConnectionNotEstablished:
No connection pool with id primary found.
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:882:in `retrieve_connection'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_handling.rb:128:in `retrieve_connection'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_handling.rb:91:in `connection'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/fixtures.rb:516:in `create_fixtures'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/fixtures.rb:1015:in `load_fixtures'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/fixtures.rb:988:in `setup_fixtures'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/fixtures.rb:852:in `before_setup'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-rails-3.6.1/lib/rspec/rails/adapters.rb:126:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:375:in `execute_with'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:606:in `block (2 levels) in run_around_example_hooks_for'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:342:in `call'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:607:in `run_around_example_hooks_for'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:464:in `run'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:457:in `with_around_example_hooks'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:500:in `with_around_and_singleton_context_hooks'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:251:in `run'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:627:in `block in run_examples'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:623:in `map'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:623:in `run_examples'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:589:in `run'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:590:in `block in run'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:590:in `map'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:590:in `run'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:118:in `block (3 levels) in run_specs'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:118:in `map'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:118:in `block (2 levels) in run_specs'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/configuration.rb:1894:in `with_suite_hooks'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:113:in `block in run_specs'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/reporter.rb:79:in `report'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:112:in `run_specs'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:87:in `run'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:71:in `run'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:45:in `invoke'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/exe/rspec:4:in `<top (required)>'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/bin/rspec:23:in `load'
# /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/bin/rspec:23:in `<top (required)>'
# /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `load'
# /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `kernel_load'
# /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:24:in `run'
# /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli.rb:304:in `exec'
# /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
# /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
# /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch'
# /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start'
# /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli.rb:11:in `start'
# /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/exe/bundle:27:in `block in <top (required)>'
# /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/friendly_errors.rb:98:in `with_friendly_errors'
# /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/exe/bundle:19:in `<top (required)>'
# /vagrant/easypost_admin/vendor/bundle/bundle:23:in `load'
# /vagrant/easypost_admin/vendor/bundle/bundle:23:in `<main>'
#
# Showing full backtrace because every line was filtered out.
# See docs for RSpec::Configuration#backtrace_exclusion_patterns and
# RSpec::Configuration#backtrace_inclusion_patterns for more information.
Finished in 0.0058 seconds (files took 2.29 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/controllers/application_controller_spec.rb:15 # ApplicationController the truth false not equal true
UPDATE
rails_help.rb
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
# ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
# 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 = false
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
end
spec_helper.rb
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end
config.shared_context_metadata_behavior = :apply_to_host_groups
end
I had the very same issue with a rails 5.2.0.alpha app without a database
configured. So I had a look at the rspec-rails code, and figured out this
line which causes all the trouble:
rspec-rails/lib/rspec/rails/fixture_support.rb:
if defined?(ActiveRecord::TestFixtures)
It checks for a loaded ActiveRecord constant, which actually was loaded. I
also had require 'active_record/railtie' commented out from
config/application.rb. Then I figured out who required the ActiveStorage gem
(bundle.lock). In my case it was ActiveStorage.
I just use the ActiveStorage functionality without a database, so I had to
remove the ActiveRecord constant on my spec/rails_helper.rb:
# frozen_string_literal: true
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Remove the ActiveRecord constant, because it is autloaded by
# ActiveStorage and not needed for our application. The presence
# of the ActiveRecord constant causes rspec-rails to include
# extra fixture support, which results in:
#
# ActiveRecord::ConnectionNotEstablished:
# No connection pool with 'primary' found.
#
Object.send(:remove_const, :ActiveRecord)
Mind the order, it needs to be placed after
require File.expand_path('../../config/environment', __FILE__)
and before
require 'rspec/rails'
This solution just modify the test environment.
UPDATE
I create an issue on rspec-rails which targets the creation of a global rspec configuration to turn off ActiveRecord handling.
A fix for this has just been merged into rspec-rails, and will supposedly be released along with RSpec Rails 4.0 sometimes next week.
What you have to set the following in your spec/rails_helper.rb:
config.use_active_record = false
Again, the problem is that if you didn't configure/use the database connection, but ActiveRecord is still present (due to Bundler loading it or some other reasons).
I'm trying to write a simple test but my application is not detecting 'users' keyword when I'm using it in my Test functions:
post_controller_test.rb:
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
test "should fetch facebook post" do
sign_in users(:one)
get(:save_posts_from_facebook_page,{'id'=>'my_id'},{'access_token'=>Rails.application.secrets.fb_access_token})
assert_response :success
end
end
test_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
include Devise::TestHelpers
# Add more helper methods to be used by all tests here...
end
users.yml:
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
email: hello#testing.com
encrypted_password: <%= Devise.bcrypt(User,'password') %>
links:
causes:
two:
email: MyString
password: MyString
links:
causes:
Output of rake/Error:
Run options: --seed 42684
# Running:
E
Finished in 0.010275s, 97.3279 runs/s, 0.0000 assertions/s.
1) Error:
PostsControllerTest#test_should_fetch_facebook_post:
NoMethodError: undefined method `users' for #<PostsControllerTest:0x000001042152f0>
test/controllers/posts_controller_test.rb:9:in `block in <class:PostsControllerTest>'
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
Update:
Note that I'm using MongoId.
When I add 'fixtures :all' to my test_helper.rb, I get:
rake
rake aborted!
NoMethodError: undefined method `fixtures' for ActiveSupport::TestCase:Class
/Users/gautambajaj/My Stuff/FreeFromBorders/f2b_website/test/test_helper.rb:7:in `<class:TestCase>'
/Users/gautambajaj/My Stuff/FreeFromBorders/f2b_website/test/test_helper.rb:5:in `<top (required)>'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/My Stuff/FreeFromBorders/f2b_website/test/controllers/posts_controller_test.rb:1:in `<top (required)>'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
Tasks: TOP => test:run
(See full trace by running task with --trace)
In your test_helper.rb file, you need to add fixtures :all:
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# rest of the codes in this file
end
Once you add that line in your test_helper.rb file as shown above, your test will pass.
Update
As Sergio Tulentsev mentioned in the comment, Mongodb doesn't support transactions, so transactional fixtures can't be used in your case. Take a look at this answer which states that and also this google group discussion as well.
I would recommend you to use factory_girl instead, which supports mongoid and is pretty awesome.
I'm trying to do something seemingly simple but it's proven rather hard.
I want to write tests using RSpec for classes that I've put in the lib directory of a Rails Engine.
Here are exactly the steps I'm using:
rails plugin new mygem -T --mountable --full --dummy-path=spec/dummy
Then I cd mygem; vim mygem.gemspec
I add the following line to mygem.gemspec:
s.add_development_dependency "rspec-rails"
I run bundle install; rails generate rspec:install
Then I edit ~/mygem/lib/mygem/engine.rb adding the following:
module Mygem
class Engine < ::Rails::Engine
isolate_namespace Mygem
config.generators do |g|
g.test_framework :rspec
end
end
end
I create a very simple class in the lib directory, ~/mygem/lib/mygem/something.rb
and add the following:
module Mygem
class Something
def hi
"hi"
end
end
end
Create a test file ~/mygem/spec/something_spec.rb
then add the following:
require 'rails_helper'
describe Mygem::Something do
it 'says hi' do
s = Mygem::Something.new
expect(s.hi).to eq('hi')
end
end
And boom, I get the following output:
rspec
~/Documents/mygem/spec/rails_helper.rb:3:in `require': cannot load such file -- ~/Documents/mygem/config/environment (LoadError)
from ~/Documents/mygem/spec/rails_helper.rb:3:in `<top (required)>'
from ~/Documents/mygem/spec/something_spec.rb:1:in `require'
from ~/Documents/mygem/spec/something_spec.rb:1:in `<top (required)>'
from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `load' from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
from ~/.rvm/gems/ruby-2.2.2/bin/rspec:23:in `load'
from ~/.rvm/gems/ruby-2.2.2/bin/rspec:23:in `<main>'
from ~/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval'
from ~/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>'
Any tips as to what I may be doing incorrectly?
Thanks in advance!
It looks like your rails_helper is looking for an environment.rb file to load, which doesn't exist in a Rails engine. It does, however, exist in your dummy app, which is what RSpec is run against.
Try adding this into the top of your rails_helper file:
require File.expand_path("../dummy/config/environment.rb", __FILE__)
Finally got it working after the following steps (thanks ccai for the suggestion):
in rails_helper.rb:
# Comment this line:
# require File.expand_path('../../config/environment', __FILE__)
require File.expand_path("../dummy/config/environment.rb", __FILE__)
then in lib/mygem.rb
require "mygem/something"
Works!
I have been trying to use RSpec with Rails 4 but came across this error that I can't get rid of.
However, when I run test on single *.rb files it works without any errors. I use guard and have tried to run rspec in single mode but received the same error.
13:46:26 - INFO - Running: spec
/Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/application.rb:214:in `initialize!': Application has been already initialized. (RuntimeError)
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /Users/xxx/Documents/Mitt_bolag/Minnesota/beställningssite/order_site/config/environment.rb:5:in `<top (required)>'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `block in require'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
from /Users/xxx/Documents/Mitt_bolag/Minnesota/beställningssite/order_site/spec/spec_helper.rb:3:in `<top (required)>'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `block in require'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
from /Users/xxx/Documents/Mitt_bolag/Minnesota/beställningssite/order_site/spec/requests/products_spec.rb:1:in `<top (required)>'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `load'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `block in load'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `load'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:896:in `each'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/command_line.rb:22:in `run'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:80:in `run'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:17:in `block in autorun'
This is 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 'rspec/autorun'
require 'capybara/rspec'
# 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 }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
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
# 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
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.include Devise::TestHelpers, :type => :controller
end
class ActionController::TestCase
include Devise::TestHelpers
end
I ran into this problem while upgrading a Rails 4.2 and Ruby 2.2 app to Rails 4.2 and Rails 2.6. Apparently, ActionController::TestResponse is reused and therefore initialized multiple times. You can add a workaround reading this thread, or simply create spec/support/action_controller_workaround.rb:
if RUBY_VERSION>='2.6.0'
if Rails.version < '5'
class ActionController::TestResponse < ActionDispatch::TestResponse
def recycle!
# hack to avoid MonitorMixin double-initialize error:
#mon_mutex_owner_object_id = nil
#mon_mutex = nil
initialize
end
end
else
puts "Monkeypatch for ActionController::TestResponse no longer needed"
end
end
Try setting your spec_helper to 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'
require 'capybara/rspec' # NOTE: I DON'T have this but you do...
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
end
I don't know if you've already got the solution, but here I go anyway:
I'm facing the same problem since yesterday. I've found that running a
single spec by time or removing the 'require "spec_helper" from all specs but one,
it would work.
And a few minutes ago, I've found that the problem was the ruby version.
If you upgrade your ruby-2.0.0-p0 to ruby-2.0.0-p247, then the problem goes away!
I hope I've been helpful.
I'm trying to run tests with Devise and Rspec and have been unsuccessful thus far - any help would be greatly appreciated.
rake spec:requests --trace
gives me this error
`/Users/carl/.rvm/rubies/ruby-1.9.2-head/bin/ruby -S rspec ./spec/requests/users_spec.rb
/Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:503:in ‘load_missing_constant': Expected /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/devise-2.1.2/app/controllers/devise_controller.rb to define DeviseController (LoadError)
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in ‘block in const_missing'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in ‘each'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in ‘const_missing'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/devise-2.1.2/app/controllers/devise/sessions_controller.rb:1:in ‘<top (required)>'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘require'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘block in require'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in ‘load_dependency'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘require'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:359:in ‘require_or_load'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:502:in ‘load_missing_constant'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in ‘block in const_missing'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in ‘each'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in ‘const_missing'
from /Users/carl/rails_projects/my_rails_app_name/app/controllers/application_controller.rb:14:in ‘<class:ApplicationController>'
from /Users/carl/rails_projects/my_rails_app_name/app/controllers/application_controller.rb:1:in ‘<top (required)>'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘require'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘block in require'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in ‘load_dependency'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘require'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:359:in ‘require_or_load'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:502:in ‘load_missing_constant'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in ‘block in const_missing'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in ‘each'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in ‘const_missing'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:229:in ‘block in constantize'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:228:in ‘each'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:228:in ‘constantize'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/core_ext/string/inflections.rb:54:in ‘constantize'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/devise-2.1.2/app/controllers/devise_controller.rb:2:in ‘<top (required)>'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘require'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘block in require'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in ‘load_dependency'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘require'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:359:in ‘require_or_load'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:502:in ‘load_missing_constant'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in ‘block in const_missing'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in ‘each'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in ‘const_missing'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/devise-2.1.2/app/controllers/devise/confirmations_controller.rb:1:in ‘<top (required)>'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘require'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘block in require'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in ‘load_dependency'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘require'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:359:in ‘require_or_load'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:313:in ‘depend_on'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:225:in ‘require_dependency'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/engine.rb:439:in ‘block (2 levels) in eager_load!'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/engine.rb:438:in ‘each'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/engine.rb:438:in ‘block in eager_load!'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/engine.rb:436:in ‘each'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/engine.rb:436:in ‘eager_load!'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/application/railties.rb:8:in ‘each'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/application/railties.rb:8:in ‘all'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/engine.rb:434:in ‘eager_load!'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/application/finisher.rb:53:in ‘block in <module:Finisher>'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/initializable.rb:30:in ‘instance_exec'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/initializable.rb:30:in ‘run'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/initializable.rb:55:in ‘block in run_initializers'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/initializable.rb:54:in ‘each'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/initializable.rb:54:in ‘run_initializers'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/application.rb:136:in ‘initialize!'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/railtie/configurable.rb:30:in ‘method_missing'
from /Users/carl/rails_projects/my_rails_app_name/config/environment.rb:5:in ‘<top (required)>'
from /Users/carl/rails_projects/my_rails_app_name/spec/spec_helper.rb:3:in ‘require'
from /Users/carl/rails_projects/my_rails_app_name/spec/spec_helper.rb:3:in ‘<top (required)>'
from /Users/carl/rails_projects/my_rails_app_name/spec/requests/users_spec.rb:1:in ‘require'
from /Users/carl/rails_projects/my_rails_app_name/spec/requests/users_spec.rb:1:in ‘<top (required)>'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in ‘load'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in ‘block in load_spec_files'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in ‘map'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in ‘load_spec_files'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in ‘run'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in ‘run'
from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in ‘block in autorun'
rake aborted!
/Users/carl/.rvm/rubies/ruby-1.9.2-head/bin/ruby -S rspec ./spec/requests/users_spec.rb failed
/Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/rake_task.rb:137:in ‘block (2 levels) in initialize'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/lib/rake/file_utils_ext.rb:60:in ‘verbose'
/Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/rake_task.rb:127:in ‘block in initialize'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in ‘call'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in ‘block in execute'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in ‘each'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in ‘execute'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/lib/rake/task.rb:158:in ‘block in invoke_with_call_chain'
/Users/carl/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/monitor.rb:201:in ‘mon_synchronize'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in ‘invoke_with_call_chain'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/lib/rake/task.rb:144:in ‘invoke'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/lib/rake/application.rb:116:in ‘invoke_task'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in ‘block (2 levels) in top_level'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in ‘each'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in ‘block in top_level'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in ‘standard_exception_handling'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/lib/rake/application.rb:88:in ‘top_level'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/lib/rake/application.rb:66:in ‘block in run'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in ‘standard_exception_handling'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in ‘run'
/Users/carl/.rvm/gems/ruby-1.9.2-head#global/gems/rake-0.9.2.2/bin/rake:33:in ‘<top (required)>'
/Users/carl/.rvm/gems/ruby-1.9.2-head/bin/rake:19:in ‘load'
/Users/carl/.rvm/gems/ruby-1.9.2-head/bin/rake:19:in ‘<main>'
`
Gemfile for :development, :test
gem 'sqlite3'
gem 'rspec-rails'
gem 'capybara'
spec/requests/user_spec.rb
> `require 'spec_helper'
>
> describe "Users" do
> describe "GET /" do
> it "goes to homepage" do
> visit '/'
> response.status.should be(200)
> end
> end
> end`
spec/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 'rspec/autorun'
require 'capybara/rspec'
#require 'capybara/rails'
# Note: To create integration test: $rails g integration_test blah --integration-tool=rspec
# 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|
# Include devise in Rspec tests
config.include Devise::TestHelpers, :type => :controller
# ## 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
# 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
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
end
I don't know exactly what is going wrong, but a quick search for LoadError and the "Expected ... to define" pattern brought up a few relevant discussions.
Expected ... to Define
Rails 3.1 — 'load_missing_contant': Expected ... to define ... (LoadError)
Expected x.rb to define X (LoadError)
Rails: Why does RSpec crash on my overridden Devise controller request spec?
From the first link:
The error message is misleading. Expected foo.rb to define Foo usually
just means that something bad happened when loading foo.rb (syntax
errors, missing constant etc...). You can usually get a better error
message by just typing Foo into script/console
The error is coming directly from Devise, so perhaps there is some naming conflict in your controllers with the devise controllers? I don't use devise so I don't really know much about how it works. Although I doubt that it's the problem, have a look at the file /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/devise-2.1.2/app/controllers/devise_controller.rb and make sure it's not corrupted. You might try to reinstall the gem in rvm.
A friend helped me figure this one out. In my application controller I was overriding Devise with the below code. Commenting this out allowed rake spec:requests to pass successfully.
# ===============================================
# == Commented this out to make rspec tests work
# ===============================================
# Override Devise create session controller
# Devise::SessionsController.class_eval do
# def create
# resource = warden.authenticate!(auth_options)
# set_flash_message(:notice, :signed_in) if is_navigational_format?
# sign_in(resource_name, resource)
# #sign_in_path = after_sign_in_path_for(resource)
# end
# end
Thank you again for your time and help!