Reputation: 539
I use mini-test
for testing framework. I use omniauth
gem for authentication. I use simplecov
for code coverage. I run my tests using "bundle exec rake"
or "rake minitest:controllers"
. I give an example for controllers. When I run rake minitest:controllers
, controllers code coverage becomes 100%. But, when I run bundle exec rake
, controllers code coverage become 60%.
SessionsController.rb code:
class SessionsController < ApplicationController
def create
auth = request.env["omniauth.auth"]
person=Person.find_by_provider_and_uid(auth.provider,auth.uid) || Person.create_with_omniauth(auth)
redirect_to root_path
end
end
SessionsController_test.rb
require "minitest_helper"
describe SessionsController do
before do
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:identity]
@person = Fabricate.build(:person)
end
it "should create authentication" do
assert_difference('Person.count') do
post :create, :provider => "identity"
end
assert_redirected_to root_path @person
end
end
I wonder that if I miss one point on writing test. I wait your ideas. Thanks in advance.
minitest_helper.rb
require 'simplecov'
Simplecov.start
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require "minitest/autorun"
require "minitest/rails"
require "minitest/pride"
require 'database_cleaner'
require "minitest/rails/capybara"
require "minitest-mongoid"
DatabaseCleaner[:mongoid].strategy = :truncation
#OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:identity, {
:uid => '12345'
})
class MiniTest::Spec
before :each do
DatabaseCleaner.start
end
after :each do
DatabaseCleaner.clean
end
end
Upvotes: 6
Views: 4462
Reputation: 2850
According to Simplecov's documentation, you just have to add theses lines in top of your test/test_helper.rb
:
# test/test_helper.rb
require 'simplecov'
SimpleCov.start
# ...
Also do not forget to install simplecov gem in test group:
# Gemfile
# ...
group :test do
gem 'simplecov'
end
And that's it.
Rails 6: I encountered some issues with Rails 6 and tests paralelization so you may deactivate it in test/test_helper.rb
:
# test/test_helper.rb
# ...
class ActiveSupport::TestCase
# ...
# parallelize(workers: 2)
end
Upvotes: 3
Reputation: 11174
It's hard to tell with no more information.
First of all try rake minitest:all
and update your question with the result.
Please try following in case the former test did not conclude positively:
namespace :test do
task :coverage do
require 'simplecov'
SimpleCov.start 'rails' # feel free to pass block
Rake::Task["test"].execute
end
end
Let us know and we can edit or update the answer.
Minitest is known to have had some issues with it. I believe it was still work in progress, not sure where they stand now. It is not you, it's minitest. That workaround helped in some cases, maybe it helps you too.
Upvotes: 2