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

这些错误可能是由于您在为 RSpec 2 编写测试时使用了 RSpec 3,但您应该能够安全地忽略它们。正如 RSpec文档中所解释的那样,忽略消息应该在那里。

于 2014-08-16T18:52:44.693 回答
0

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

foo.should == bar

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

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

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

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

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

于 2014-08-16T19:18:56.967 回答