Techioz Blog

Ruby-rspec - テスト (グループ) 名を出力する方法

概要

何かテストがある場合、例えば

  require_relative "Line"
  require_relative "LineParser"

  describe Line do

    it "Can be created" do
      load "spec_helper.rb"
      @line.class.should == Line
    end 
    it "Can be parsed" do
    ...

この場合、テストグループ名「Line」を出力するにはどうすればよいですか。

追加してみました:

    before :all do
      puts "In #{self.class}"
    end

しかし、それは次のようになります: LineではなくRSpec::Core::ExampleGroup::Nested_3

解決策

テスト中にテスト名にアクセスしたいという特別な理由があるかもしれません…ただし、テスト レポートに行を出力するだけのニーズに合う場合に備えて、私は次の構成を好みます。

RSpec.configure do |config|

   # Use color in STDOUT
  config.color = true

  # Use color not only in STDOUT but also in pagers and files
  config.tty = true

  # Use the specified formatter
  config.formatter = :documentation 

end

これにより、次のような出力が得られます。

MyClassName
  The initialization process
    should accept two optional arguments