0

私は TestFirst.org の Learn Ruby チュートリアルを行っており、現在演習番号 3 の Simon Says に取り組んでいます。これが問題なのか、それとも rspec を使用したテストに干渉している (そして間違った結果が得られている) のかはわかりませんが、すべてのテストに合格したことを示す前に、ターミナルにいくつかのエラー メッセージが表示されます。

caitlyns-mbp:03_simon_says caitlynyu$ rake
(in /Users/caitlynyu/Desktop/learn_ruby)
/Users/caitlynyu/Desktop/learn_ruby/03_simon_says/simon_says_spec.rb:62: warning: possibly useless use of == in void context
/Users/caitlynyu/Desktop/learn_ruby/03_simon_says/simon_says_spec.rb:63: warning: possibly useless use of == in void context

Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}

Simon says
  repeat
    should repeat
    should repeat a number of times
  echo
    should echo hello
    should echo bye
  start_of_word
    returns the first letter
    returns the first several letters
    returns the first two letters
  shout
    should shout hello
    should shout multiple words
  first_word
    tells us the first word of 'Hello World' is 'Hello'
    tells us the first word of 'oh dear' is 'oh'
  titleize
    doesn't capitalize 'little words' in a title
    does capitalize 'little words' at the start of a title
    capitalizes every word (aka title case)
    capitalizes a word

Finished in 0.00324 seconds
15 examples, 0 failures

Randomized with seed 36160

simon_says_spec.rb に対して警告が表示されますが、これは TestFirst Learn Ruby チュートリアルで提供されている仕様です。なぜこれに問題があるのですか?また、「すべての例が除外されました。{:focus=>true|}' を無視しますか?

4

2 に答える 2

0

問題の 1 つは、rspec の演算子マッチャーにあります。

foo.should == bar

それはあなたが得た警告を生成したということです。Ruby は、== が返されていないか、etc に含まれていないため、役に立たないifと考えています。通常の比較を行っているだけで、結果を使用していないと考えています。ruby がこの警告チェックを行う時点で、rspec が==によって返されたオブジェクトを再定義したこと、shouldおよび結果としてそれが役に立たないことを認識していません。

この警告を回避するにはeq、rspec 2.12 で導入された expect 構文の有無にかかわらず、マッチャーを代わりに使用できます。

foo.should eq(bar)
expect(foo).to eq(bar)

警告をオフにすることもできます (rspec 3 ではデフォルトで .rspec ファイルの警告がオンになりますが、これは元に戻されていると思います)。

フィルタリングに関する警告は問題ありません。

于 2014-08-16T19:18:56.967 に答える