【问题标题】:TestFirst Learn Ruby Simon SaysTestFirst 学习 Ruby Simon 说
【发布时间】:2014-08-16 18:20:39
【问题描述】:

我正在学习 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|}'?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      问题之一在于 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 文件中的警告,但我相信这正在恢复)

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-13
        相关资源
        最近更新 更多