When I run rspec spec I get the following:
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails/extensions/active_record/base.rb:26:in `': uninitialized constant ActiveModel (NameError)
from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails/extensions.rb:1:in `require'
from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails/extensions.rb:1:in `'
from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails.rb:8:in `require'
from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails.rb:8:in `'
from /Users/noahc/Dropbox/perfect_setup/spec/spec_helper.rb:4:in `require'
from /Users/noahc/Dropbox/perfect_setup/spec/spec_helper.rb:4:in `'
from /Users/noahc/Dropbox/perfect_setup/spec/controllers/pages_controller_spec.rb:1:in `require'
from /Users/noahc/Dropbox/perfect_setup/spec/controllers/pages_controller_spec.rb:1:in `'
from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in `load'
from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in `block in load_spec_files'
from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in `map'
from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in `load_spec_files'
from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/command_line.rb:18:in `run'
from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:80:in `run_in_process'
from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:69:in `run'
from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:10:in `block in autorun'
My spec_helper.rb looks like this:
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require 'rspec/rails'
require 'rspec/autorun'
require 'spork'
Spork.prefork do
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
ActiveSupport::Dependencies.clear
end
end
Spork.each_run do
load "#{Rails.root}/config/routes.rb"
Dir["#{Rails.root}/app/**/*.rb"].each {|f| load f}
end
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
end
Dir[Rails.root.join('spec/support/**/*.rb')].each{|f| require f}
If I delete my pages_controller_spec file the errors go away. But I have zero specs to run.
The error returns when I have something even as simple as this:
require 'spec_helper'
describe PagesController do
it "should do something..."
end
Also, if I add any tests no matter where I get the same error.
Any clues on other things I can try?
Why are you running rspec spec? The typical thing to run is rake spec.
Try bundle exec rspec spec or bundle exec rake spec.
If that doesn't work, try to see if something is wrong with the rest of your environment -- try bundle exec rails console and bundle exec rails server
Another thing to check, is there anything odd in .rspec in your project file, or ~/.rspec?
Found this thread while debugging the same symptoms. Here is another reason for the error: I had run
rspec --init
but had failed to run
rails generate rspec:install
Doing the latter fixed the problem.
In my case
gem "spork-rails", "~> 4.0.0"
was missing in ./Gemfile.
Related
I'm really annoying this problem about this.
Error is like this.
Despite of declaration of TestLoginController, I always trapped same error.
What should I do?
$ bin/rspec
Running via Spring preloader in process 18113
/Users/noriakitakamizawa/dola/spec/controller/test_login_spec.rb:3:in `<top (required)>': uninitialized constant TestLoginController (NameError)
from /Users/noriakitakamizawa/dola/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `block in load_spec_files'
from /Users/noriakitakamizawa/dola/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `each'
from /Users/noriakitakamizawa/dola/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `load_spec_files'
Here's the code.
spec/controller/test_login_spec.rb
require 'rails_helper'
describe TestLoginController do
login_admin
it "should have a current_user" do
# note the fact that you should remove the "validate_session" parameter if this was a scaffold-generated controller
expect(subject.current_user).to_not eq(nil)
end
it "should get index" do
# Note, rails 3.x scaffolding may add lines like get :index, {}, valid_session
# the valid_session overrides the devise login. Remove the valid_session from your specs
get 'index'
response.should be_success
end
end
spec/rails_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'devise'
require 'rspec/autorun'
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, :type => :controller
# 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
And here's another helper.
spec/spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rails'
require 'devise'
Rails.logger = Logger.new(STDOUT)
RSpec.configure do |config|
config.include Rails.application.routes.url_helpers
config.include Capybara::DSL
# for Devise
#config.include Devise::TestHelpers, :type => :controller
#config.include Capybara::DSL
config.include Devise::Test::ControllerHelpers, :type => :controller
I try to test the login methods for sorcery gem and i get an error.
I user factory girl for factories in my rspec tests.
All i want to do is to add a before method that logs me in, and after that i want to test the actions for my controller.
My spec_helper.rb
require 'factory_girl'
require_relative '../spec/factories/blog.rb'
require_relative '../spec/factories/user.rb'
require_relative '../spec/factories/category.rb'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.include Sorcery::TestHelpers::Rails::Controller, type: :controller
config.include Sorcery::TestHelpers::Rails::Integration, type: :feature
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
my rails_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
end
the error i get when i run my rspec
Running: spec
/home/user/work/project_name/spec/spec_helper.rb:9:in `block in <top (required)>': uninitialized constant Sorcery::TestHelpers::Rails::Controller (NameError)
My work environment:
Rails 3.2, Ruby 1.9.3, rspec - rails 3.3, sorcery 0.8.2
I found the problem after hours of pain.
The problem was that for sorcery version 0.8.2 you need to add to spec_helper.rb the following code:
RSpec.configure do |config|
.................................(other stuff)
config.include Sorcery::TestHelpers::Rails
..........................(other stuff)
end
and after that use in your specs
#user = User.create
login_user(#user)
I have a simple rails app which Im trying to introduce elementary tests in. Im following this url.
My application is structured as follows:
Im trying to test the single page: called Main Page.
In spec/features/main_pages_spec.rb I have the following code:
require 'spec_helper'
feature "soadevise" do
feature "Main Page" do
scenario "should have the content 'Main Page' "
visit '/main_page/home'
expect(page).to have_content('Main Page')
end
end
My spec/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 }
# 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
config.include Capybara::DSL
# 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
When I run this command in the terminal:
MRMIOMP0903:soadevise $ bundle exec rspec spec/features/main_pages_spec.rb
I get the following error:
/Users/am/Desktop/x/xx/rails_projects/mysql_apps/soadevise/
spec/features/main_pages_spec.rb:6:
in `block (2 levels) in <top (required)>': undefined method `visit' for #
<Class:0x007fb7237377b0> (NoMethodError)
from /Users/am/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core 2.14.7/lib/rspec/core/example_group.rb:246:in `module_eval'
from /Users/am.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:246:in `subclass'
from /Users/am.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:232:in `describe'
My gemfile contains these:
#For testing
group :development, :test do
gem "rspec-rails", "~> 2.14.1"
end
group :test do
gem "selenium-webdriver"
gem "capybara"
end
I refered to this link as well.
Can someone suggest what Im doing wrong?
You're calling feature twice. It should look like this:
require 'spec_helper'
feature "soadevise Main Page" do
scenario "should have the content 'Main Page'" do
visit '/main_page/home'
expect(page).to have_content('Main Page')
end
end
I'm having an inexplicably hard time getting my named routes to work within my rspec tests.
Here's what I'm seeing:
1) Home GET / works!
Failure/Error: get root_path
NameError:
undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fe13b0c1538>
# ./spec/requests/home_spec.rb:6:in `block (3 levels) in <top (required)>'
2) Home GET / shows products!
Failure/Error: get products_path
NameError:
undefined local variable or method `products_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fe13b0cdea0>
# ./spec/requests/home_spec.rb:10:in `block (3 levels) in <top (required)>'
spec/requests/home_spec.rb
require 'spec_helper'
describe "Home" do
describe "GET /" do
it "works!" do
get root_path
response.status.should be(200)
end
it "shows products!" do
get products_path
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'
# 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}
require 'spree/core/testing_support/factories'
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.before(:suite) do
# DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
# DatabaseCleaner.start
end
config.after(:each) do
# DatabaseCleaner.clean
end
config.include Rails.application.routes.url_helpers
end
You can see that the test is including spec_helper, and spec_helper is including the routing methods. Here's my rake routes:
bundle exec rake routes
...
root / spree/home#index
products GET /products(.:format) spree/products#index
...
Any idea what could be going on here to prevent the routes from working? Thanks!
If you're wanting to reference the route of an engine from tests within an dummy application, or just an application itself, then you'll need to prefix the routing helpers with the engine name. For Spree, you would reference the routes like:
spree.products_path
Similarly, if you want to reference routes of the main application from an engine, you'll need to use main_app:
main_app.root_path
I have one spec:
require 'spec_helper'
# hmm... I need to include it here because if I include it inside describe block `method_missing` exception raises.
include Shoulda::ActionController::Matchers
describe CategoriesController do
include Devise::TestHelpers
render_views
context "new should render template :new" do
setup do
sign_in :user
get :new
end
should render_template(:new)
end
end
when I run rake spec I see this(but if i change context to it and move setup block contents to it block all works fine):
localhost:gallery rtest$ rake spec
(in /Users/rtest/Projects/gallery)
/Users/rtest/.rvm/rubies/ruby-1.9.2-p0/bin/ruby -S bundle exec /Users/rtest/.rvm/rubies/ruby-1.9.2-p0/bin/ruby -Ilib -Ispec "./spec/controllers/categories_controller_spec.rb" "./spec/controllers/photos_controller_spec.rb" "./spec/helpers/categories_helper_spec.rb" "./spec/helpers/photos_helper_spec.rb" "./spec/models/category_spec.rb"
***
Pending:
CategoriesHelper add some examples to (or delete) /Users/rtest/Projects/gallery/spec/helpers/categories_helper_spec.rb
# Not Yet Implemented
# ./spec/helpers/categories_helper_spec.rb:14
PhotosHelper add some examples to (or delete) /Users/rtest/Projects/gallery/spec/helpers/photos_helper_spec.rb
# Not Yet Implemented
# ./spec/helpers/photos_helper_spec.rb:14
Category add some examples to (or delete) /Users/rtest/Projects/gallery/spec/models/category_spec.rb
# Not Yet Implemented
# ./spec/models/category_spec.rb:4
Finished in 0.0006 seconds
3 examples, 0 failures, 3 pending
/Users/rtest/.rvm/gems/ruby-1.9.2-p0/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/render_template_matcher.rb:41:in `renders_template?': undefined method `assert_template' for #<Class:0x00000104839eb0> (NoMethodError)
from /Users/rtest/.rvm/gems/ruby-1.9.2-p0/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/render_template_matcher.rb:23:in `matches?'
from /Users/rtest/.rvm/gems/ruby-1.9.2-p0/gems/rspec-expectations-2.0.0.beta.20/lib/rspec/expectations/handler.rb:11:in `handle_matcher'
from /Users/rtest/.rvm/gems/ruby-1.9.2-p0/gems/rspec-expectations-2.0.0.beta.20/lib/rspec/expectations/extensions/kernel.rb:27:in `should'
from ./spec/controllers/categories_controller_spec.rb:17:in `block (2 levels) in <main>'
from /Users/rtest/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/example_group.rb:129:in `module_eval'
from /Users/rtest/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/example_group.rb:129:in `subclass'
from /Users/rtest/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/example_group.rb:116:in `describe'
from ./spec/controllers/categories_controller_spec.rb:11:in `block in <main>'
from /Users/rtest/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/example_group.rb:129:in `module_eval'
from /Users/rtest/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/example_group.rb:129:in `subclass'
from /Users/rtest/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/example_group.rb:116:in `describe'
from /Users/rtest/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/extensions/object.rb:7:in `describe'
from ./spec/controllers/categories_controller_spec.rb:6:in `<main>'
Loaded suite /Users/rtest/.rvm/gems/ruby-1.9.2-p0/bin/rake
Started
Finished in 0.003418 seconds.
0 tests, 0 assertions, 0 failures, 0 errors, 0 skips
Test run options: --seed 39993
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 'shoulda'
require 'shoulda/rails'
require 'shoulda/integrations/rspec2'
# 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
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, comment the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
end
I am using rails 3. rspec/shoulda/factory_girl are included to the application by this code in Gemfile:
group :test, :development do
gem 'autotest'
gem 'factory_girl'
gem "shoulda"
gem "rspec-rails", ">= 2.0.0.beta.20"
end
I'm pretty sure that error is because you haven't wrapped your shoulda assertion inside an it block like so:
it { should render_template(:new) }
This is required when using Shoulda with RSpec.
should render_template(:new) on it's own will work with Test::Unit.
If I'm not mistaken, I've used Shoulda and I think the syntax is
should_render_template(:new)
and not
should render_template(:new)
which explains the 0 tests,0 assertions...