I would be very grateful for some advice on the following.
Below I have a simplified example for our setup.
I have defined some Helper modules on our RSpec test suite:
## spec/rails_helper.rb
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config|
config.include ViewHelpers
end
### spec/support/view_helpers/base.rb
# frozen_string_literal: true
module ViewHelpers
class Base
# class content
end
end
### spec/support/view_helpers/admins/payments/new.rb
# frozen_string_literal: true
module ViewHelpers
module Admins
module Payments
class New < Base
# class content
end
end
end
end
With this setup the specs run perfectly locally; the sub-class New inherits from Base as expected. However, when the tests are running on Travis CI, we get the following error:
NameError:
1433 uninitialized constant ViewHelpers::Admins::Payments::Base
1434# ./spec/support/view_helpers/admins/payments/new.rb:6:in `<module:Payments>'
1435# ./spec/support/view_helpers/admins/payments/new.rb:5:in `<module:Admins>'
1436# ./spec/support/view_helpers/admins/payments/new.rb:4:in `<module:ViewHelpers>'
1437# ./spec/support/view_helpers/admins/payments/new.rb:3:in `<top (required)>'
1438# ./spec/rails_helper.rb:34:in `block in <top (required)>'
1439# ./spec/rails_helper.rb:34:in `each'
1440# ./spec/rails_helper.rb:34:in `<top (required)>'
1441# ./spec/controllers/admins/billpayers_controller_spec.rb:3:in `<top (required)>'
I have also tried inheriting with explicit namespacing:
# frozen_string_literal: true
module ViewHelpers
module Admins
module Payments
class New < ViewHelpers::Base
# class content
end
end
end
end
Again, this runs successfully locally but Travis CI errors:
NameError:
1433 uninitialized constant ViewHelpers::Base
1434# ./spec/support/view_helpers/admins/payments/new.rb:6:in `<module:Payments>'
1435# ./spec/support/view_helpers/admins/payments/new.rb:5:in `<module:Admins>'
1436# ./spec/support/view_helpers/admins/payments/new.rb:4:in `<module:ViewHelpers>'
1437# ./spec/support/view_helpers/admins/payments/new.rb:3:in `<top (required)>'
1438# ./spec/rails_helper.rb:34:in `block in <top (required)>'
1439# ./spec/rails_helper.rb:34:in `each'
1440# ./spec/rails_helper.rb:34:in `<top (required)>'
1441# ./spec/controllers/admins/billpayers_controller_spec.rb:3:in `<top (required)>'
I am rather stumped as to why this works locally but not on CI.
Would be very grateful for any support and insight.
Thanks
Sam
Hi did you double checked your autoload configurations? (https://guides.rubyonrails.org/autoloading_and_reloading_constants.html)
Autoload and cache classes may be configured differently based on your environment configs:
├── config/environments
├── development.rb
├── production.rb
└── test.rb
It turns out the ordering of
Dir[Rails.root.join("spec/support/**/*.rb")]
depends on the OS and filesystem.
This means the sub-classes were getting required before the super-class.
Solution:
Dir[Rails.root.join("spec/support/**/*.rb")].sort
Related
I am trying to run a single test file: bundle exec rspec spec/models/user_spec.rb
But get following error:
An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: config.include ::Rails::Controller::Testing::TemplateAssertions, type: :controller
NameError:
uninitialized constant Rails::Controller
Did you mean? ApiController
# ./spec/rails_helper.rb:149:in `block in <top (required)>'
# ./spec/rails_helper.rb:61:in `<top (required)>'
# ./spec/models/user_spec.rb:3:in `require'
# ./spec/models/user_spec.rb:3:in `<top (required)>'
No examples found.
Initially, I was getting following error:
An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: module Shoulda::Matchers::ActiveModel
NameError:
uninitialized constant Shoulda
# ./spec/support/matchers/validate_kept_of_matcher.rb:4:in `<top (required)>'
# ./spec/rails_helper.rb:51:in `block in <top (required)>'
# ./spec/rails_helper.rb:51:in `each'
# ./spec/rails_helper.rb:51:in `<top (required)>'
# ./spec/models/user_spec.rb:3:in `require'
# ./spec/models/user_spec.rb:3:in `<top (required)>'
No examples found.
But then it fixed after I added following to the test file.
require "shoulda/matchers"
I am newbie in Ruby/Rails world, can someone please give a direction?
Rails::Controller::Testing::TemplateAssertions was removed in Rails 5.
You can re-add the depreciated functionality by installing the Rails controller testing gem. However the use of controller specs, assigns and template assertions is discouraged by both the RSpec and Rails teams and is not very future proof.
The community accepted solution is to write request specs and stop poking inside your controllers.
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'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!
I create a custom class which I call MyClass in a module MyModule
module MyModule
class MyClass
def initialize
... # Some code here
end
end
end
I save this code in a file called mymodule.rb
I place this file in the lib directory of my rails application, and add the following line to my application.rb
config.autoload_paths += %W(#{config.root}/lib)
When I fire up the rails console and try to use this file. it just doesnt work.
m = MyModule::MyClass.new()
NameError: uninitialized constant MyModule
from (irb):1
from /Users/matt/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.6/lib/rails/commands/console.rb:44:in `start'
from /Users/matt/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.6/lib/rails/commands/console.rb:8:in `start'
from /Users/matt/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.6/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
What do I need to do in order to be able to use that class in the rails console
Any help appreciated
Name the file my_class.rb and place it in the directory my_module. That should fix your problem.
When I run:
ruby -I test test/unit/job_test.rb
from the application root directory, I got the following error:
.../app/models/name_position.rb:9:in `<class:NamePosition>':
uninitialized constant NamePosition::PositionManager (NameError)
from .../app/models/name_position.rb:1:in `<top (required)>'
from .../app/models/ac_buyer.rb:1:in `<top (required)>'
...
Here is the relevant code:
# app/models/ac_buyer.rb
class AcBuyer < NamePosition
end
# app/models/name_position.rb
class NamePosition < ActiveRecord::Base
...
include PositionManager
end
# lib/position_manager.rb
module PositionManager
...
end
It looks like when running tests, the files from the lib directory are not loaded automatically.
Any suggestions ?
Try putting this in your application.rb
config.autoload_paths += %W(#{config.root}/lib)