36
35

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SimpleCovを利用してコードのカバレッジを計測する。

Posted at

ともあれインストール。

Gemfile
ENV['NOKOGIRI_USE_SYSTEM_LIBRARIES'] = 'YES'
gem "rspec", "~> 2.14.1"
gem "simplecov", "~> 0.8.0"
$ bundle install --path vendor/bundle

RSpecの初期化をする。

$ bundle exec rspec --init
create   spec/spec_helper.rb
create   .rspec

Bundlerでインストールしたディレクトリをフィルタリングして対象外にする。

spec_helper.rb
require 'simplecov'

SimpleCov.start do
    add_filter "/vendor/"
end

RSpec.configure do |config|
  config.treat_symbols_as_metadata_keys_with_true_values = true
  config.run_all_when_everything_filtered = true
  config.filter_run :focus
  config.order = 'random'
end

テストを書く。

spec/calc_spec.rb
require 'spec_helper'
require 'car'

describe Car do
    it "car named ferrari" do
        car = Car.new
        expect(car.name).to eq "ferrari"
    end

    it "car price 200" do
        car = Car.new
        expect(car.price).to eq 200
    end
end

実装する。

lib/car.rb
class Car
    attr_accessor :name, :price

    def initialize(name="ferrari")
        @name = name
        @price = 200
    end
end

テストを実行をする。

$ bundle exec rspec
..

Finished in 0.03427 seconds
2 examples, 0 failures

Randomized with seed 36271

Coverage report generated for RSpec to /vagrant/simplecov/coverage. 5 / 5 LOC (100.0%) covered.

WEBrickを起動する。

webrick.rb
require 'webrick'

params = {
    :DocumentRoot => './coverage',
    :BindAddress => '0.0.0.0',
    :Port => 8080
}

srv = WEBrick::HTTPServer.new(params)

trap(:INT){ srv.shutdown }
srv.start
$ ruby webrick.rb

結果を表示する。

Screen Shot 2014-09-10 at 7.09.43 AM.png

参考リンク

36
35
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
35

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?