I'm trying to run a test file for a controller, but I'm running into this error:
/home/mariana/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require': cannot load such file -- ./controllers/users_controller
(LoadError)
I think rspec can't find the actual files it's supposed to test.
The test file, users_controller_spec.rb:
require 'rails_helper'
require './controllers/users_controller'
describe UsersController do
describe '#create' do
...
end
end
The header/preamble of rails_helper.rb, that deals with file paths (I think):
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'
The section of the Gemfile that deals with rspec and development/test-specific stuff:
group :development, :test do
gem 'spring'
gem 'rspec', '~>3.5'
gem 'rspec-rails', '~>3.5'
end
The project tree:
->project
->app
->controllers
->users_controller.rb
->spec
->controllers
->users_controller_spec.rb
->rails_helper.rb
->spec_helper.rb
I've tried messing around with the require File.expand_path(...) line on rails_helper.rb. I also tried adding the rspec gem to the Gemfile alongside rspec-rails (the problem also existed when it was just rspec-rails).
Any suggestions?
I solved it by changing the path of the controller in the test from this:
require './controllers/users_controller.rb'
To this:
require './app/controllers/users_controller.rb'.
I already suspected it was a file path error, so I just tried different relative paths until one of them worked.
Related
I am working on a project with Rails 4 & Mongoid 4. I am trying to set up Shoulda-matchers (version 2.8.0), following thoughtbot/shoulda-matchers, which points to another README called README for 2.8.0. And I am hoping to use the mongoid-rspec for testing.
However I keep getting spec_helper.rb:94:in '<top (required)>': uninitialized constant Shoulda (NameError)
I added this in Gemfile: following thoughtbot/shoulda-matchers
group :test do
gem 'shoulda-matchers'
end
I also added in spec_helper.rb (Which is where the error comes from) - following thoughtbot/shoulda-matchers
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
I have tried googling but there is no direct solution to this (Or there is no direct problem to this). I added require: false in the Gemfile, following README for 2.8.0
group :test do
gem 'shoulda-matchers', require: false
end
I added require 'shoulda/matchers' in the rails_helper.rb. My order of 'requires' is this:
require 'spec_helper'
require 'rspec/rails'
require 'shoulda/matchers'
require 'rspec/rails' is below require 'spec_helper' by default. And by the README provided on the github page, I should place shoulda\matcher below 'rspec/rails'. I have also tried to place require 'shoulda/matchers' on top of require 'spec_helper' but it didn't work.
My versions:
Rails 4.2.1
Ruby 2.2.1
Mongoid ~ 4.0.0
rspec-rails ~ 3.0
mongoid-rspec ~ 2.1.0
shoulda-matchers 2.8.0
I really appreciate any help.
From the link you provided:
NOTE: The new configuration syntax isn't available in a public release just yet -- please refer to the README for 2.8.0 for the current installation instructions.
That note is from the master branch. The current release (2.8.0) has a different set of documentation. Confusing, I know.
Just remove that configuration section from spec/spec_helper.rb and all should be rainbows and unicorns once again.
Just insert these lines into spec/spec_helper.rb:
config.include(Shoulda::Matchers::ActiveModel, type: :model)
config.include(Shoulda::Matchers::ActiveRecord, type: :model)
Reference:
https://github.com/thoughtbot/shoulda-matchers#availability-of-matchers-in-various-example-groups
I'm testing for validation of entry of a city attribute in an addresses model. My model spec is as follows
require 'rails_helper'
RSpec.describe Address, :type => :model do
before(:each) do
#valid_attributes = {
street: 'rock avenue',
city: 'MSA',
zip: '00100',
person_id: 1
} # runs before each it block
end
context "Validations" do
it 'must have a city' do
a = Address.new
expect(a.errors_on(:city)).not_to be_empty
end
end
end
When I run the spec, i get the following error
1) Address Validations must have a city
Failure/Error: expect(a.errors_on(:city)).not_to be_empty
NoMethodError:
undefined method `errors_on' for #<Address:0xd844538>
# ./spec/models/address_spec.rb:19:in `block (3 levels) in <top (required)>'
My test gems are set up as following in my Gemfile
group :development, :test do
gem 'rspec-rails'
gem 'factory_girl_rails', :require => false
end
group :test do
gem 'capybara'
gem 'guard'
gem 'guard-rspec'
gem 'libnotify'
gem 'rspec-collection_matchers'
end
I have included the rspec collection matchers in my spec_helper, so I don't understand why I am getting this error
My spec_helper.rb
require 'capybara/rspec'
require 'factory_girl_rails'
require 'rspec/collection_matchers'
However I can see that the method, errors_on has been defined in rspec-collection_matchers gem as shown here
My .rspec
--color
--require spec_helper
I have also changed the position of
require 'rspec/collection_matchers'
to rails_helper.rb, but running the test suite again brings up the same error
rails_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require 'rspec/collection_matchers'
require 'capybara/rspec'
require 'factory_girl_rails'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.include Capybara::DSL
config.include FactoryGirl::Syntax::Methods
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
end
My spec_helper.rb is empty with only the block below:
RSpec.configure do |config|
end
I am using Rails 4.1.0, rspec 3.0.0 and rspec-collection_matchers 1.0.0
Without seeing the repo, this is only a guess based on the information. I think this is a load order problem.
It looks like you are loading rspec/collection_matchers in spec_helper, not rails_helper. If you are using the new default configuration, --require spec_helper is in your .rspec file. When you run rspec this will require spec_helper very early on. When that happens it requires rspec/collection_matchers. However, at that point rails_helper has not been loaded. Unfortunately, this also means most of ActiveSupport including ActiveModel are not loaded yet. It also means the Rails autoloading functionality has not been loaded, so those won't get defined when you reference them.
All this to say, the check for ActiveModel here returns false. Which does not load errors_on.
Try moving the require into rails_helper.
Note on testing in isolation:
If you are going for faster testing in isolation this is my suggestion:
spec_helper should be kept very minimal and lightweight, that means for the most part you shouldn't add any require declarations to it
Move the requires you listed from spec_helper into rails_helper, those are mostly Rails specific and do not belong in spec_helper as Rails is not loaded there
If you need access to the other rspec/collection_matchers functionality in specs which do not require Rails, then just add the require 'rspec/collection_matchers' to the top of the spec file; Ruby will handle making sure it is only loaded once
I'm new to ROR and wondering if Simplecov provides tests coverage for both TestUnit and RSpec tests in the same project?
I am in the process of migrating some tests we wrote in RSpec from a standalone webapp to an existing webapp that currently uses TestUnit.
I know that both RSpec and TestUnit can live in the same project, but I haven't been able to see how Simplecov will generate coverage for both test frameworks.
When I run on the command line bundle exec rake spec I get:
Coverage report generated for RSpec to /.../coverage. 0 / 0 LOC (0.0%) covered.
when I run bundle exec rake test I get test coverage generated.
The plan is to migrate everything from TestUnit to rspec but with 400+ tests we want to have both TestUnit and RSpec tests covered by Simplecov whilst we migrate to TestUnit.
I'm sure there must be something incorrect in my configuration
Any help would be much appreciated!
I have the following set up:
--- Rakefile ---
require File.expand_path('../config/application', __FILE__)
require 'rake'
require 'simplecov'
if Rails.env == 'test' || Rails.env == 'development'
require 'ci/reporter/rake/test_unit'
require 'ci/reporter/rake/rspec'
end
SimpleCov.start 'rails' do
add_filter "/test/"
add_filter "/spec/"
end
-- spec/spec_helper.rb --
ENV["RAILS_ENV"] ||= 'test'
require 'simplecov'
SimpleCov.start 'rails'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
-- test/test_helper.rb --
require 'simplecov'
SimpleCov.start 'rails' do
add_filter "/vendor/"
end
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
This doesn't answer your question directly, but if it helps, I have a bash script that does a lot of the work for you for porting from TestUnit to RSpec. It's at https://github.com/Noreaster76/porter. You could use it to convert your TestUnit files, and after cleaning up the results a bit, you wouldn't have the issue of having to run both RSpec and TestUnit.
I'm trying to write a rakefile outside of a rails project.
I've created a new directory, added a rakefile and set a basic default task. It works.
I want to use the premailer gem so in my default task I've added this -
premailer = Premailer.new('http://localhost/email.html', :warn_level => Premailer::Warnings::SAFE)
This doesn't work, I get the following error.
uninitialized constant Object::Premailer
How do I refer to the premailer gem in my task? Should I be including it in a gemfile of sorts?
You can work without a Gemfile like this:
require 'rubygems' # only needed for ruby 1.8.7
require 'premailer'
desc "My Task"
task :my_task do
..
premailer = Premailer.new(...)
...
end
or with a Gemfile:
require 'rubygems' # only needed for ruby 1.8.7
require 'bundler'
Bundler.setup
Bundler.require
desc "My Task"
task :my_task do
..
premailer = Premailer.new(...)
...
end
I hope this helps.
when I run rspec spec/models result is OK.
But when I use spork, every test where shoulda macros (like it { should validate_presence_of(:title) } is used FAILS with error like: undefined method 'validate_presence_of' for ...
I use:
rails (3.0.0)
shoulda (2.11.3)
spork (0.8.4)
rspec-rails (>= 2.0.0.beta.22)
spec/spec_helper.rb:
require 'rubygems'
require 'spork'
Spork.prefork do
# 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'
...
I had the same issue. Fixed it by sticking require 'shoulda/integrations/rspec2' after requiring rspec/rails in prefork block.
You might also want to upgrade your spork to the latest version (gem 'spork', >= 0.9.0.rc2), since I didn't try this fix on 0.8.4 (although I am pretty sure it'll work too)
Try moving the
require "shoulda"
line into the Spork.each_run block. Apparently, shoulda does some magic to include the matchers into the appropriate example groups.