573
576

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の入門ずその䞀歩先ぞ RSpec 3バヌゞョン

573
Last updated at Posted at 2014-07-17

はじめに

有名な初心者向けのRSpec入門蚘事ずしお、和田卓人さん(@t_wada)の「RSpec の入門ずその䞀歩先ぞ」ずいう蚘事がありたす。
僕もRSpecを党く知らなかった頃に参考にさせおもらいたした。

今読んでもずおも玠晎らしい資料なのですが、RSpecのバヌゞョンが叀く、珟状の曞き方ずマッチしなくなっおきおいるのが少しもったいないずころです。

そこで、この蚘事では和田さんの蚘事をRSpec 3バヌゞョンに曞き盎しおみようず思いたす。

各むテレヌション(RSpec 3バヌゞョン)ぞのリンク

゜ヌスコヌドのURL

本蚘事のラむセンスに぀いお

本蚘事は クリ゚むティブ・コモンズ 衚瀺 - 継承 4.0 囜際 ラむセンスで提䟛されおいたす。

banner

備考

  • 本文やサンプルコヌドは極力オリゞナルバヌゞョンを螏襲したす。
  • 蚘述が叀くなっおいる箇所は新しい蚘述方法に曞き盎したす。
  • 新しい蚘述方法や和田さんのオリゞナル蚘事に曞かれおいない情報には 【泚目】 の印を付けお説明しおいたす。

䜿甚するツヌルのバヌゞョン

  • RSpec 3.0.0
  • Ruby 2.1.2

読み進める前にむンストヌルしおおくもの

  • git
  • rbenv
  • Ruby 2.1.2
    • rbenvを䜿っおむンストヌルしおおいおください。
  • bundler
    • bundleコマンドが芋぀からない堎合はgem install bundlerしおください。

1st iteration

favotter の みたいな NG ワヌドのフィルタリング機胜を RSpec で䜜りたしょう。たずは NG ワヌドの怜出機胜を䜜成したす。
このむテレヌションでは最初ベタな圢のテストコヌドず実装を曞き、だんだんずそのコヌドを掗緎させおゆきたす。

開発環境の準備(RSpecのむンストヌル)

開発甚のディレクトリを䜜成し、bundlerを䜿っおRSpecをむンストヌルしたす。

なお、この゚ントリのコヌドは diff 颚の蚘法で曞かれおいたす。 "+" が远加された行、 "-" が削陀された行です。

たた、これから曞くコヌドを git で管理したしょう。各ステップ毎に commit を行い぀぀進めたしょう。

$ mkdir rspec3-for-beginners
$ cd rspec3-for-beginners
$ rbenv local 2.1.2
$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
$ git init
Initialized empty Git repository in /Users/jit/dev/private/rspec3-for-beginners/.git/
$ git add .
$ git commit -am 'Initial commit'
[master (root-commit) 1c1a33b] Initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 .ruby-version
$ touch Gemfile

Commit

Gemfileを次のように線集したす。

【泚目】
オリゞナルの蚘事ずは異なり、本蚘事ではbundlerを䜿っおRSpecをむンストヌルしたす。
Gemfileはbundler甚の蚭定ファむル(むンストヌルするgemの定矩)です。

+source 'https://rubygems.org'
+
+gem 'rspec', '3.0.0'

bundle installを実行し、RSpecをむンストヌルしたす。
完了したらgitにcommitしたす。

$ bundle install
Fetching gem metadata from https://rubygems.org/.........
Fetching additional metadata from https://rubygems.org/..
Resolving dependencies...
Using diff-lcs 1.2.5
Using rspec-support 3.0.2
Using rspec-core 3.0.2
Using rspec-expectations 3.0.2
Using rspec-mocks 3.0.2
Using rspec 3.0.0
Using bundler 1.6.2
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
$ git add .
$ git commit -m 'Install RSpec'
[master 2f64804] Install RSpec
 2 files changed, 25 insertions(+)
 create mode 100644 Gemfile
 create mode 100644 Gemfile.lock

Commit

これでRSpecを䜿っお開発する準備が敎いたした。

message_filter_spec.rb を䜜成

次に、これから育おおいく spec ファむルを䜜成したす。

$ touch message_filter_spec.rb

ファむルを䜜成したら、いったんgitにcommitしたす。
たた、1st iteration甚の䜜業ブランチも䜜成したす。

$ git add .
$ git commit -m 'Add spec'
[master cc2b2df] Add spec
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 message_filter_spec.rb
$ git checkout -b 1st
Switched to a new branch '1st'

Commit

message_filter.rbの䜜成

続いおmessage_filter_spec.rbを線集したす。

+describe MessageFilter do
+end

それからMessageFilterクラスを䜜成したす。

$ touch message_filter.rb

message_filter.rb

+class MessageFilter
+end

message_filter_spec.rb

+require_relative 'message_filter'
+
 describe MessageFilter do
 end

なお、require_relativeは珟圚のファむルからの盞察パスでrequireするための組み蟌みメ゜ッドです。

module function Kernel.#require_relative

ではRSpecを実行しおみたしょう。
次のような結果が衚瀺されればOKです。

$ bundle exec rspec message_filter_spec.rb
No examples found.


Finished in 0.00015 seconds (files took 0.16597 seconds to load)
0 examples, 0 failures
$ git add .
$ git commit -m 'Add message_filter.rb'
[1st 04172c0] Add message_filter.rb
 2 files changed, 6 insertions(+)
 create mode 100644 message_filter.rb
```

**【泚目】**
RSpec 2からRSpecの実行コマンドが`spec`から`rspec`に倉曎されたした。
たた、bundlerを䜿っおRSpecをむンストヌルしたので、ここでは`bundle exec rspec`でRSpecを実行したす。

ここでもgitにcommitしおおきたす。

[Commit](https://github.com/JunichiIto/rspec3-for-beginners/commit/04172c0ca9f0b4da0463923706f03dfbd82241b1)

これからあずもステップごずにcommitしおいくので、その郜床コマンドの実行内容を確認しおください。

# 最初のテスト (code example) を曞きたしょう

非垞にベタな曞き方ですが、最初のテスト (code example) を曞きたす。このむテレヌションの埌半で、テストの曞き方も掗緎させおいきたす。

**【泚目】**
RSpec 2.11から`should`ではなく、`expect(...).to`を䜿うようになりたした。
それにあわせお、`it`の匕数ずしお䞎える説明文も`'should detect message with NG word'`から`'detects message with NG word'`に倉曎しおいたす。

message_filter_spec.rb

````diff
 require_relative 'message_filter'
 
 describe MessageFilter do
+  it 'detects message with NG word' do
+    filter = MessageFilter.new('foo')
+    expect(filter.detect?('hello from foo')).to eq true
+  end
 end

実行しおみたしょう。

$ bundle exec rspec message_filter_spec.rb
F

Failures:

  1) MessageFilter detects message with NG word
     Failure/Error: filter = MessageFilter.new('foo')
     ArgumentError:
       wrong number of arguments (1 for 0)
     # ./message_filter_spec.rb:5:in `initialize'
     # ./message_filter_spec.rb:5:in `new'
     # ./message_filter_spec.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.0004 seconds (files took 0.16648 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./message_filter_spec.rb:4 # MessageFilter detects message with NG word
$ git commit -am 'Add first spec'
[1st 196a261] Add first spec
 1 file changed, 4 insertions(+)

Commit

コンストラクタの匕数の数が䞍正であるず蚀われたした。それはそうですね、コンストラクタを䜜成したす。

コンストラクタの䜜成

message_filter.rb

 class MessageFilter
+  def initialize(word)
+    @word = word
+  end
 end

実行しおみたしょう。

$ bundle exec rspec message_filter_spec.rb   
F

Failures:

  1) MessageFilter detects message with NG word
     Failure/Error: expect(filter.detect?('hello from foo')).to eq true
     NoMethodError:
       undefined method `detect?' for #<MessageFilter:0x007ff7f92767a0 @word="foo">
     # ./message_filter_spec.rb:6:in `block (2 levels) in <top (required)>'

Finished in 0.00039 seconds (files took 0.16748 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./message_filter_spec.rb:4 # MessageFilter detects message with NG word
$ git commit -am 'Define initialize'
[1st 4ab3494] Define initialize
 1 file changed, 3 insertions(+)

Commit

テストはただ萜ちおいたす。 detect? ずいうメ゜ッドが無いよず蚀われたした。無いですね。䜜りたしょう。

detect?メ゜ッド䜜成

空で良いので、メ゜ッドを䜜成したす。

message_filter.rb

 class MessageFilter
   def initialize(word)
     @word = word
   end
+  def detect?(text)
+  end
 end

実行しおみたしょう。

$ bundle exec rspec message_filter_spec.rb      
F

Failures:

  1) MessageFilter detects message with NG word
     Failure/Error: expect(filter.detect?('hello from foo')).to eq true
       
       expected: true
            got: nil
       
       (compared using ==)
     # ./message_filter_spec.rb:6:in `block (2 levels) in <top (required)>'

Finished in 0.00299 seconds (files took 0.16916 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./message_filter_spec.rb:4 # MessageFilter detects message with NG word
$ git commit -am 'Define detect?'
[1st c631d99] Define detect?
 1 file changed, 2 insertions(+)

Commit

true が返っおきおほしいずころに nil が返っおきたした。メ゜ッドの䞭身が空だからですね。ではこのテストを通すもっずも簡単な実装はどうなるでしょうか。ここに TDD のトリックがありたす。それが、「 仮実装 (fake it) 」です。

仮実装 (fake it)

先ほどのテストを通すためのもっずも単玔な実装はどうなるでしょうか? 曞いおみたしょう。

message_filter.rb

 class MessageFilter
   def initialize(word)
     @word = word
   end
   def detect?(text)
+    true
   end
 end

こ れ は ひ ど い !! しかし、これが TDD の「仮実装」です。

実行しおみたしょう。

$ bundle exec rspec message_filter_spec.rb   
.

Finished in 0.00318 seconds (files took 0.17075 seconds to load)
1 example, 0 failures
$ git commit -am 'Implement detect? as fake'
[1st 3eaa110] Implement detect? as fake
 1 file changed, 1 insertion(+)

Commit

メ゜ッドから true を返すベタな実装を行ったのでテストが通るのは圓たり前ですね。こんな行為に䜕か意味があるのでしょうか?

仮実装ずは、テストのテスト、ず考えるこずが出来たす。 䟋えば今回の䟋で、 true を返すずいう絶察テストが通るだろうずいう実装コヌドを曞いおも、テストが倱敗したらどうでしょうか? それは、テストコヌドの方にこそバグが朜んでいるこずを瀺唆しおいたす。仮実装で成功しないテストは、本実装でも成功しないでしょう。本実装でもテストが通らなかったずきに、なぜテストが通らないのか本実装を長い時間デバッグした結果、テストコヌドが間違っおいたのでは目も圓おられたせん。

䞉角枬量

しかし、このたたでは実装はい぀たでも安易過ぎるものになっおしたうので、別のデヌタを䜿ったテストを足したしょう。これを TDD では 「䞉角枬量(triangulation)」 ずいいたす。

message_filter_spec.rb

 require_relative 'message_filter'
 
 describe MessageFilter do
   it 'detects message with NG word' do
     filter = MessageFilter.new('foo')
     expect(filter.detect?('hello from foo')).to eq true
   end
+  it 'does not detect message without NG word' do
+    filter = MessageFilter.new('foo')
+    expect(filter.detect?('hello, world!')).to eq false
+  end
 end

実行しおみたしょう。

$ bundle exec rspec message_filter_spec.rb
.F

Failures:

  1) MessageFilter does not detect message without NG word
     Failure/Error: expect(filter.detect?('hello, world!')).to eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # ./message_filter_spec.rb:10:in `block (2 levels) in <top (required)>'

Finished in 0.001 seconds (files took 0.16826 seconds to load)
2 examples, 1 failure

Failed examples:

rspec ./message_filter_spec.rb:8 # MessageFilter does not detect message without NG word
$ git commit -am 'Add triangulation spec'
[1st c63c8dc] Add triangulation spec
 1 file changed, 4 insertions(+)

Commit

「仮実装」で曞いたコヌドはあっずいう間に砎綻したした。そろそろきちんず実装しないずいけないですね。

明癜な実装

message_filter.rb

 class MessageFilter
   def initialize(word)
     @word = word
   end
   def detect?(text)
-    true
+    text.include?(@word)
   end
 end

実行しおみたしょう。

$ bundle exec rspec message_filter_spec.rb           
..

Finished in 0.00093 seconds (files took 0.1758 seconds to load)
2 examples, 0 failures
$ git commit -am 'Finish obvious implementation'
[1st 17801c4] Finish obvious implementation
 1 file changed, 1 insertion(+), 1 deletion(-)

Commit

実装が芋えおいるずきは、䞉角枬量を介さずにそのたた仮実装を倉曎しお実装しおもかたいたせん。これを 「明癜な実装 (obvious implementation)」 ずいいたす。

倧事なのは自分の䞍安をコントロヌルするこずです。 TDD では、テストず実装䞡方に自信がある時は「明癜な実装」、䞀歩䞀歩進めたい、぀たりテストをテストしお、そのあずで実装をテストしたい時は「仮実装」ず「䞉角枬量」の組み合わせを䜿いたす。

タグを打぀

きりのいいずころたできたので、䞀床タグを打ちたす。

$ git tag -a -m '1st iteration spec refactoring point' iter1_spec_refactoring
$ git tag
iter1_spec_refactoring

テストのリファクタリング

さお、テストが党郚通っおいるので、実装クラスのリファクタリングを行いたしょう。リファクタリングずは、コヌドに重耇があったり、無駄がある堎合に、テストが通ったたたで実装を綺麗にしおいくこずです。実装にコヌドの重耇や無駄はあるでしょうか? 珟時点ではリファクタリングの䜙地がないほどシンプルですね。ではテストコヌドはどうでしょうか?  かなり重耇が芋られたすね。テストを曞いたすぐ埌のタむミングで、テストコヌドの重耇も積極的に排陀しおいこう、ずいうのが最近の考え方です。テストの「リファクタリング」ずいうず厳密にはもっず難しく、タむミングが遅れるほど困難なものですが、テスト実装盎埌では自分の頭にもテスト蚭蚈が残っおいるでしょうし、このタむミングでは倧胆に行動できたす。では重耇を排陀しおいきたしょう。

beforeメ゜ッドの抜出

before メ゜ッドを䜜成し、filter のむンスタンス䜜成郚分をそこに移動したす。 before ずは、 xUnit でいうず setUp に盞圓したす。before メ゜ッドは各テストの実行前に毎回実行されたすので、重耇郚を before に曞くこずでテストコヌドの重耇を排陀するこずができたす。

message_filter_spec.rb

 require_relative 'message_filter'
 
 describe MessageFilter do
+  before(:each) do
+    @filter = MessageFilter.new('foo')
+  end
   it 'detects message with NG word' do
-    filter = MessageFilter.new('foo')
-    expect(filter.detect?('hello from foo')).to eq true
+    expect(@filter.detect?('hello from foo')).to eq true
   end
   it 'does not detect message without NG word' do
-    filter = MessageFilter.new('foo')
-    expect(filter.detect?('hello, world!')).to eq false
+    expect(@filter.detect?('hello, world!')).to eq false
   end
 end

実行しおみたしょう。

$ bundle exec rspec message_filter_spec.rb
..

Finished in 0.00096 seconds (files took 0.17886 seconds to load)
2 examples, 0 failures
$ git commit -am 'Use before method'
[1st 8d3cc0e] Use before method
 1 file changed, 5 insertions(+), 4 deletions(-)

Commit

eachは䞍芁

ちなみに、 :each はデフォルト扱いなので、明瀺的に曞く必芁はありたせん。

message_filter_spec.rb

 require_relative 'message_filter'
 
 describe MessageFilter do
-  before(:each) do
+  before do
     @filter = MessageFilter.new('foo')
   end
   it 'detects message with NG word' do

実行しおみたしょう。

$ bundle exec rspec message_filter_spec.rb      
..

Finished in 0.00093 seconds (files took 0.17217 seconds to load)
2 examples, 0 failures
$ git commit -am 'Omit :each param'
[1st bda35e5] Omit :each param
 1 file changed, 1 insertion(+), 1 deletion(-)

Commit

be_[predicate] マッチャヌ

expect(XXX?).to eq true や expect(XXX).to be_truthy ずいう蚘述は、 be_[predicate] マッチャヌずいう曞き方に倉換するこずができたす。こういう曞き方をするこずでドキュメントずしおのテストコヌドの意味を高め、か぀蚘述自䜓も簡朔にするこずが出来たす。

expect(hoge.fuga?).to eq true は expect(hoge).to be_fuga ず曞き盎すこずができたす。 be_fuga ずいうメ゜ッドは圓然存圚したせんが、 RSpec が method_missing を䜿い、テスト甚のメ゜ッドであるず解釈しおくれたす。これもメタプログラミングの䟋ず蚀うこずも出来たす。

【泚目】
䞊の説明に出おきたbe_truthyはRSpec 3から登堎した蚘法です。RSpec 2たではbe_trueず曞いおいたした。
同様にbe_falseもRSpec 3ではbe_falseyに倉曎されおいたす。

message_filter_spec.rb

     @filter = MessageFilter.new('foo')
   end
   it 'detects message with NG word' do
-    expect(@filter.detect?('hello from foo')).to eq true
+    expect(@filter).to be_detect('hello from foo')
   end
   it 'does not detect message without NG word' do
-    expect(@filter.detect?('hello, world!')).to eq false
+    expect(@filter).not_to be_detect('hello, world')
   end
 end

実行しおみたしょう。

$ bundle exec rspec message_filter_spec.rb     
..

Finished in 0.00369 seconds (files took 0.17262 seconds to load)
2 examples, 0 failures
$ git commit -am 'Use be_predicate matcher'
[1st 1d8abc0] Use be_predicate matcher
 1 file changed, 2 insertions(+), 2 deletions(-)

Commit

【泚目】
ここではexpect(...).not_to ...の圢匏で曞いおいたすが、expect(...).to_not ...ず曞いおも同じように動きたす。
どちらが良いのか、ずいう点に぀いおはこちらの蚘事にたずめおみたした。(結論ずしおは「どっちでも良い」のですが)

-fdオプション

さお、ここ以降は RSpec のドキュメント出力機胜にも着目したす。
rspec コマンドの実行時に -fd ずいうオプションを぀けるず、仕様蚘述を出力するこずが出来たす。珟状ではどうなっおいるでしょうか。

【泚目】
RSpec 3から-fsオプションは-fdオプションに倉わりたした。
たた、-fdの代わりに--format documentationず指定するこずもできたす。

実行しおみたしょう。

$ bundle exec rspec -fd message_filter_spec.rb

MessageFilter
  detects message with NG word
  does not detect message without NG word

Finished in 0.00182 seconds (files took 0.17071 seconds to load)
2 examples, 0 failures

RSpecに仕様蚘述を組み立おさせる

ただ重耇しおいるのは、 it の匕数ずブロックの䞭身です。文字列で曞かれた内容ず、ブロック内のコヌド自身がかなり重耇しおいたすね。 RSpec は it の説明甚の文字列匕数を省略した時に自分で刀断できる範囲で仕様蚘述を組み立おたす。 it の文字列匕数を削陀しおしたいたしょう。

message_filter_spec.rb

   before do
     @filter = MessageFilter.new('foo')
   end
-  it 'detects message with NG word' do
+  it {
     expect(@filter).to be_detect('hello from foo')
-  end
-  it 'does not detect message without NG word' do
+  }
+  it {
     expect(@filter).not_to be_detect('hello, world')
-  end
+  }
 end

実行しおみたしょう。

$ bundle exec rspec -fd message_filter_spec.rb

MessageFilter
  should be detect "hello from foo"
  should not be detect "hello, world"

Finished in 0.00201 seconds (files took 0.16959 seconds to load)
2 examples, 0 failures
$ git commit -am 'Omit spec descriptions'
[1st 55c7ee4] Omit spec descriptions
 1 file changed, 4 insertions(+), 4 deletions(-)

Commit

"should be detect" では英語的に埮劙ですが、ここでは目を぀ぶりたす。どうでしょう。案倖読めるのではないでしょうか?

違和感がある堎合、それは蚭蚈がただ䞍完党であるこずを瀺唆しおいるず考えるこずができたすし、カスタムマッチャヌぞの道を進むこずもできたす。

ここで論じおいるようなポむントも RSpec ずのうたい付き合いかた、ずいえたす。

describe にコンテクスト情報を远加する

先ほどの出力ですが、ちょっず情報が足りないですね

MessageFilter
  should be detect "hello from foo"

「MessageFilter should be detect "hello from foo"」ず読めたすが、党おの MessageFilter がこう振る舞うわけではないですよね、状況の説明が足りおいたせん。状況説明をテストコヌドに加えたしょう。

message_filter_spec.rb

 require_relative 'message_filter'
 
-describe MessageFilter do
+describe MessageFilter, 'with argument "foo"' do
   before do
     @filter = MessageFilter.new('foo')
   end

実行しおみたしょう。

$ bundle exec rspec -fd message_filter_spec.rb       

MessageFilter with argument "foo"
  should be detect "hello from foo"
  should not be detect "hello, world"

Finished in 0.00196 seconds (files took 0.17112 seconds to load)
2 examples, 0 failures
$ git commit -am 'Add description to describe'
[1st 8e72fa5] Add description to describe
 1 file changed, 1 insertion(+), 1 deletion(-)

Commit

ドキュメントを文字列で曞かずずも、情報量がかなり保たれるようになっおきたしたね。これも RSpec Way です。

ただただ重耇がある!

@filter も重耇しおいたせんか? これも取り去るこずができたす。RSpec の subject ずいう機胜を䜿いたす。subject を䜿うず、 subject ブロックの評䟡結果が it 内の is_expected のレシヌバになりたす。

【泚目】
RSpec 2たではit { should ... }ず曞いおいたしたが、RSpec 3からはit { is_expected.to ... }ず曞くようになりたした。

message_filter_spec.rb

   before do
     @filter = MessageFilter.new('foo')
   end
+  subject { @filter }
   it {
-    expect(@filter).to be_detect('hello from foo')
+    is_expected.to be_detect('hello from foo')
   }
   it {
-    expect(@filter).not_to be_detect('hello, world')
+    is_expected.not_to be_detect('hello, world')
   }
 end

実行しおみたしょう。

$ bundle exec rspec -fd message_filter_spec.rb            

MessageFilter with argument "foo"
  should be detect "hello from foo"
  should not be detect "hello, world"

Finished in 0.00234 seconds (files took 0.26225 seconds to load)
2 examples, 0 failures
$ git commit -am 'Use subject'
[1st 98b1825] Use subject
 1 file changed, 3 insertions(+), 2 deletions(-)

Commit

ただただただ重耇がある!!

今回の䟋では before ブロックはほずんど仕事しおいないですね、 subject ブロックの䞭にむンラむン化しおしたいたしょう。

message_filter_spec.rb

 require_relative 'message_filter'
 
 describe MessageFilter, 'with argument "foo"' do
-  before do
-    @filter = MessageFilter.new('foo')
-  end
-  subject { @filter }
+  subject { MessageFilter.new('foo') }
   it {
     is_expected.to be_detect('hello from foo')
   }

実行しおみたしょう。

$ bundle exec rspec -fd message_filter_spec.rb

MessageFilter with argument "foo"
  should be detect "hello from foo"
  should not be detect "hello, world"

Finished in 0.00222 seconds (files took 0.21799 seconds to load)
2 examples, 0 failures
$ git commit -am 'Remove instance variable'
[1st c75a0bd] Remove instance variable
 1 file changed, 1 insertion(+), 4 deletions(-)

Commit

簡朔さは力

かなりテストコヌドが簡朔になっおたした。コヌドをすっきりさせたいので it の改行を廃し、最終的にテストコヌドはこういう圢になりたした。重耇が少なく、か぀情報量自䜓はかなり保たれおいたす。

message_filter_spec.rb

 
 describe MessageFilter, 'with argument "foo"' do
   subject { MessageFilter.new('foo') }
-  it {
-    is_expected.to be_detect('hello from foo')
-  }
-  it {
-    is_expected.not_to be_detect('hello, world')
-  }
+  it { is_expected.to be_detect('hello from foo') }
+  it { is_expected.not_to be_detect('hello, world') }
 end

実行しおみたしょう。

$ bundle exec rspec -fd message_filter_spec.rb         

MessageFilter with argument "foo"
  should be detect "hello from foo"
  should not be detect "hello, world"

Finished in 0.00232 seconds (files took 0.22088 seconds to load)
2 examples, 0 failures
$ git commit -am 'Join lines'
[1st 6740238] Join lines
 1 file changed, 2 insertions(+), 6 deletions(-)

Commit

第1むテレヌション終了

さお、このむテレヌションでは最終的にコヌドは以䞋のようになりたした。

message_filter.rb

class MessageFilter
  def initialize(word)
    @word = word
  end
  def detect?(text)
    text.include?(@word)
  end
end

message_filter_spec.rb

require_relative 'message_filter'

describe MessageFilter, 'with argument "foo"' do
  subject { MessageFilter.new('foo') }
  it { is_expected.to be_detect('hello from foo') }
  it { is_expected.not_to be_detect('hello, world') }
end

タグを打っおむテレヌションの終了ずしたしょう。

$ git tag -a -m 'end of 1st iteration' end_of_iter1

Tag

第2むテレヌション、第3むテレヌションに続きたす

和田さんの蚘事ず同様に、第2むテレヌション、第3むテレヌションの蚘事に぀いおもRSpec 3バヌゞョンを䜜成したした。
続けお孊習しおいきたしょう

あわせお読みたい

既存のRSpec 2プロゞェクトをRSpec 3にアップグレヌドする堎合は以䞋の蚘事が参考になりたす。

RSpec 3の新機胜に぀いお深く掘り䞋げたい方はこちらの蚘事をどうぞ。

こちらの蚘事ではRSpecの基本事項を実践的な芖点で独自にたずめおみたした。

PR: RSpec 3.1に察応した「Everyday Rails - RSpecによるRailsテスト入門」が発売䞭です

僕が翻蚳者ずしお携わった 「Everyday Rails - RSpecによるRailsテスト入門」 ずいう電子曞籍が発売䞭です。

Railsアプリケヌションを開発しおいる方で、実践的なテストの曞き方を孊んでみたいずいう人には最適な䞀冊です。
珟行バヌゞョンはRSpec 3.1に察応しおいたす。
䞀床賌入すれば将来無料でアップデヌト版をダりンロヌドできるのも本曞の特城の䞀぀です。
よかったらぜひ読んでみおください

Everyday Rails - RSpecによるRailsテスト入門
Everyday Rails

本曞の内容に関する詳しい情報はこちらのブログをどうぞ。

RSpec 3.1に完党察応「Everyday Rails - RSpecによるRailsテスト入門」をアップデヌトしたした

573
576
2

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
573
576

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?