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.
Related
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.
I installed ansicon to make ansi colorized console output possible for minitest test feedback on windows.
I am running minitest with the minitest-reporters gem to format the output, yet whatever I do I can't get the output to show colors (all text is black).
Here's my test_helper.rb file:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use! Minitest::Reporters::ProgressReporter.new( { :color => true } )
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
Does anyone know this problem?
Reference: https://github.com/kern/minitest-reporters
I see the post is from 2015. 4 years passed since then.
Anyway, let me share how it worked for me yesterday.
My environment:
Ubuntu 18.04
rails 6.0.1
ruby 2.6.5
1) Gemfile:
gem 'minitest-reporters'
2) bundle install
3) test_helper.rb:
require "minitest/reporters"
Minitest::Reporters.use!
4) rake
I'm using RSpec for my Rails 3 tests and trying to use Spork.
I followed several tutorials and Spork seems to be running with no errors, but my tests still take the same amount of time to run (43 seconds) with Spork on and off.
How can I figure out what's going on?
Gemfile
gem 'spork', '>=0.9.0.rc9'
spec_helper.rb
require 'rubygems'
require 'spork'
require 'factory_girl'
require 'cover_me'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
# 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'
# Force find of factory girl definitions. Tests started failing without this, and the factories could not be found
Factory.find_definitions
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# Needed for Spork
ActiveSupport::Dependencies.clear
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
When I run →spork to start the server everything looks fine:
→ spork
Using RSpec
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!
I also have --drb in the .rspec file
It depends on how you measure execution time. If you are including the time it takes to spin up rails that should be faster, but actual test run time wont change.
Spork caches the rails environment which speeds up spin up time but it doesn't speed up actual execution time.
I'm using Ruby 1.9.2, Rails 3.1, Rspec, Postgres and Spork, but I can't get them to play nicely together.
Running the specs for the first time (with Spork running in the background) works fine. However, when I run the specs a second time, it fails with:
Failure/Error: Unable to find matching line from backtrace
PGError:
no connection to the server
# /Users/tom/.rvm/gems/ruby-1.9.2-p180#grapi/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/postgresql_adapter.rb:272:in `exec'
etc....
Any tips appreciated!
You probably also have Devise enabled.
Your problem is described here: https://github.com/sporkrb/spork/wiki/Spork.trap_method-Jujutsu
And more specifically for rails 3.1 here: https://gist.github.com/1054078
The beginning of your prefork block in spec_helper.rb and env.rb should looks like:
Spork.prefork do
Spork.trap_method(Rails::Application, :reload_routes!)
Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
...
Good luck!
If you're using Factory Girl, don't use the 'factory_girl_rails' gem, just use 'factory_girl'.
Spork.each_run do
FactoryGirl.definition_file_paths = [
File.join(Rails.root, 'spec', 'factories')
]
FactoryGirl.find_definitions
end
For anyone using Factory Girl, Machinist, or Shoulda Matchers, make sure you read about Spork's trap_method at: https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu
It solved my problems with Spork and the dropped PostgreSQL connections while testing.
you have to run spork --bootstrap
and after insert some configuration to your spec_helper.rb file, so spork knows about your rails configuration.
As you use RSpec, you can try adding the following code to your spec_helper file:
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
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
# Needed for Spork
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
Could you try adding this to Spork.each_run callback and check if it solves the problem?
ActiveRecord::Base.connection_pool.verify_active_connections!
I read the instructions on https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu and found the following.
In my case the solution was to change how machinist blueprints was loaded. My prefork block had this line:
Spork.prefork do
...
require Rails.root.join 'spec/support/blueprints'
...
I removed that from the prefork block and instead added this line to each_run:
Spork.each_run do
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
...
The two lines basically does the same thing, so the main thing seams to be not to load the blueprints in the prefork, but rather in each_run.
Hope it 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.