【问题标题】:What is wrong with this RSpec expectation?这个 RSpec 期望有什么问题?
【发布时间】:2014-06-03 05:22:47
【问题描述】:

我正在使用 rspec 版本 2.14.8 和 Ruby 2.1.1。

我在 test_spec.rb 中有以下内容

describe 'My code' do
  it 'should work' do
    expect ( nil ).to be_nil
    expect ( "test" ).to eq( "test")
  end
end

当我运行这个简单的规范 (rspec test_spec.rb) 时,我收到以下错误:

Failures:

 1) My code should work
    Failure/Error: expect ( nil ).to be_nil
    NoMethodError:
      undefined method `to' for nil:NilClass
    # ./test_spec.rb:3:in `block (2 levels) in <top (required)>'

怎么了!?

【问题讨论】:

    标签: ruby rspec rspec2 matcher


    【解决方案1】:

    您不能在 expect 和开头的括号 ( 之间放置空格。

    工作代码示例如下:

    describe 'My code' do
      it 'should work' do
        expect( nil ).to be_nil
        expect( "test" ).to eq( "test")
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 2012-08-24
      • 2012-05-26
      • 2021-08-20
      相关资源
      最近更新 更多