Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

minitest testing and code coverage

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.

EDIT

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
like image 621
miyamotomusashi Avatar asked Mar 13 '13 15:03

miyamotomusashi


1 Answers

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
like image 190
alexandre-rousseau Avatar answered Oct 17 '22 15:10

alexandre-rousseau