25
25

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.

RSpecのテンプレート

Posted at

=======================

会社の先輩から教えて頂いたRSpecを書くときのコツ。次のテンプレートを基本として書く。

describe '#メソッド名' do
  subject { hoge }                     # describe に対して subject を書く

  context '状況1' do
    before do                        # context に対して before を書く
    end

    it { ... }
  end
  
  context '状況2' do
    # ...
  end
end

Mock を使う場合は subject が使えないので、it の代わりに specify を使って書く。
ただ、Mock ではなく spy を使えば subject でかける。
RSpec で spy を使うには rspec-spies を用いる。

また、このテンプレートの問題点は複数のメソッドで同じ context を使いたい場合にテストが冗長になること。その場合、shared_context を使う。

25
25
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
25
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?