TestFirst学习Ruby Simon说

时间:2014-08-16 18:20:39

标签: ruby-on-rails ruby rspec test-first

我正在参加TestFirst.org的学习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 |}'?

2 个答案:

答案 0 :(得分:0)

错误可能是由于您在为RSpec 2编写测试时使用RSpec 3,但您应该能够安全地忽略它们。忽略消息应该存在,如RSpec documentation中所述。

答案 1 :(得分:0)

使用rspec的运算符匹配器的问题之一

foo.should == bar

它是否产生了你的警告。 Ruby认为==是无用的,因为它没有被返回,不在if等:它认为你只是在进行正常比较而不是使用结果。在ruby执行此警告时,检查它并没有意识到rspec已在==返回的对象上重新定义should,因此它不会无用。

要避免此警告,您可以使用eq匹配器,无论是否有rspec 2.12中引入的expect语法:

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

你也可以关闭警告(rspec 3默认打开.rspec文件中的警告,但我相信这是在恢复)

关于过滤的警告不是问题。