LoginSignup
144
140

More than 5 years have passed since last update.

rspecについてまとめてみました

Last updated at Posted at 2014-03-10

今更ですが自分がrspecを使う上で知っておきたい事をまとめておきました。

基本的な記述方法

spec_test.rb
require 'rspec_helper' #railsなら必要

# describe テスト対象のクラス
describe TestTargetClass do
  # ネストができる
  describe TestTargetSubClass do
    # itなどで共通化したい処理があったらメソッドを定義できる
    def shared_method
      puts 'shared'
    end
    # このdescribeが実行される時に行われる処理
    before :all do
      @test = TestTargetClass.new
    end
    # 各it毎に行われる事前処理
    before :each do
      @test.set_value('test')
    end
    # このdescribeが全て実行した後に行われる処理
    after :all do
      @test = nil
    end
    # 各itが終了する毎に実行される処理
    after :each do
      @test.remove_value
    end

    # classの状態を記述する 実はdescribeのaliasだがトップレベルでは宣言できない
    context 'set value' do
      # どのような振る舞いをするかを記述する 
      it 'do something'do
        # expectを使用し期待する通りの振る舞いをするか確認する
        expect(@test.value).to be 'test'
      end
    end
  end
end

順番は before(:all) before(:each) it after(:each) after(:all)で処理されます。

Matcher

続いて振る舞いを確認するmactherの一覧です。
rspec-expectationの転載です。

同じ値になるか確認

expect(actual).to eq(expected)  # passes if actual == expected
expect(actual).to eql(expected) # passes if actual.eql?(expected)

同一のオブジェクトかを確認

expect(actual).to be(expected)    # passes if actual.equal?(expected)
expect(actual).to equal(expected) # passes if actual.equal?(expected)

大小を確認

expect(actual).to be >  expected
expect(actual).to be >= expected
expect(actual).to be <= expected
expect(actual).to be <  expected
expect(actual).to be_within(delta).of(expected)

正規表現がマッチするか確認

expect(actual).to match(/expression/)

同一クラスか確認

expect(actual).to be_an_instance_of(expected) # passes if actual.class == expected
expect(actual).to be_a(expected)              # passes if actual.is_a?(expected)
expect(actual).to be_an(expected)             # an alias for be_a
expect(actual).to be_a_kind_of(expected)      # another alias

True or False

expect(actual).to be_truthy # passes if actual is truthy (not nil or false)
expect(actual).to be true   # passes if actual == true
expect(actual).to be_falsy  # passes if actual is falsy (nil or false)
expect(actual).to be false  # passes if actual == false
expect(actual).to be_nil    # passes if actual is nil

例外処理

expect { do_exception }.to raise_error
expect { do_exception }.to raise_error(ErrorClass)
expect { do_exception }.to raise_error("message")
expect { do_exception }.to raise_error(ErrorClass, "message")

例外が吐かれる事を確認

expect { do_exception }.to throw_symbol
expect { do_exception }.to throw_symbol(:symbol)
expect { do_exception }.to throw_symbol(:symbol, 'value')

yieldの確認

expect { |b| 5.tap(&b) }.to yield_control # passes regardless of yielded args
expect { |b| yield_if_true(true, &b) }.to yield_with_no_args # passes only if no args are yielded
expect { |b| 5.tap(&b) }.to yield_with_args(5)
expect { |b| 5.tap(&b) }.to yield_with_args(Fixnum)
expect { |b| "a string".tap(&b) }.to yield_with_args(/str/)
expect { |b| [1, 2, 3].each(&b) }.to yield_successive_args(1, 2, 3)
expect { |b| { :a => 1, :b => 2 }.each(&b) }.to yield_successive_args([:a, 1], [:b, 2])

値を持っているか確認

expect(actual).to be_xxx         # passes if actual.xxx?
expect(actual).to have_xxx(:arg) # passes if actual.has_xxx?(:arg)

範囲に含まれている事を確認

これはRuby 1.9 以上でのみ使用可能

expect(1..10).to cover(3)

コレクションを確認

expect(actual).to include(expected)
expect(actual).to start_with(expected)
expect(actual).to end_with(expected)
expect(actual).to contain_exactly(individual, items)
# ...which is the same as:
expect(actual).to match_array(expected_array)
144
140
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
144
140