为什么RSpec匹配器不工作?

时间:2013-05-15 15:18:52

标签: rspec rspec2

当我使用任何基本匹配器运行这个简单的RSpec时,它不起作用:

档案:matcher_spec.rb

describe "a simple RSpec matcher" do
    true.should be_true
end

使用的任何匹配器的输出如下所示:

/private/tmp/rspec_matcher_test/matcher_spec.rb:3:in `block in <top (required)>': undefined local variable or method `be_true' for #<Class:0x007fdcd60ccb40> (NameError)
    from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:242:in `module_eval'
    from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:242:in `subclass'
    from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:228:in `describe'
    from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/dsl.rb:18:in `describe'
    from /private/tmp/rspec_matcher_test/matcher_spec.rb:2:in `<top (required)>'
    from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load'
    from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
    from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
    from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
    from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
    from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
    from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'

(是的,我是answering my own question

1 个答案:

答案 0 :(得分:11)

这是因为您忘记了describe块下面的'it'块。 RSpec DSL不了解描述块范围内的匹配器。

要修复它,只需添加一个'it'块:

describe "a simple RSpec matcher" do
  it "should work now" do
    true.should be_true
  end
end

问题症状:您会看到类似以下的错误

_spec.rb:4:in `block in <top (required)>': undefined method `be' for #<Class:0x007f9ce20ea458> (NoMethodError)

_spec.rb:6:in `block in <top (required)>': undefined local variable or method `be_false' for #<Class:0x007fc59b0d2358> (NameError)

_spec.rb:7:in `block in <top (required)>': undefined local variable or method `be_nil' for #<Class:0x007fb5912afeb0> (NameError)

_spec.rb:8:in `block in <top (required)>': undefined local variable or method `be_nil' for #<Class:0x007f9a6d21f278> (NameError)

_spec.rb:18:in `block in <top (required)>': undefined local variable or method `be_empty' for #<Class:0x007fdff23260e8> (NameError)

_spec.rb:22:in `block in <top (required)>': undefined method `eq' for #<Class:0x007f9ac2a30110> (NoMethodError)

_spec.rb:26:in `block in <top (required)>': undefined method `eql' for #<Class:0x007fe45c188588> (NoMethodError)

_spec.rb:27:in `block in <top (required)>': undefined method `equal' for #<Class:0x007fc30c9a6a10> (NoMethodError)

_spec.rb:12:in `block in <top (required)>': undefined method `match' for #<Class:0x007fb142909160> (NoMethodError)

_spec.rb:13:in `block in <top (required)>': undefined method `be_within' for #<Class:0x007fe47da21728> (NoMethodError)

_spec.rb:19:in `block in <top (required)>': undefined method `have_key' for #<Class:0x007f9adc0df1c8> (NoMethodError)

_spec.rb:15:in `block in <top (required)>': undefined method `cover' for #<Class:0x007f868a304090> (NoMethodError)

_spec.rb:16:in `block in <top (required)>': undefined method `expect' for #<Class:0x007fbe62a8b2e0> (NoMethodError)

_spec.rb:14:in `include': wrong argument type Fixnum (expected Module) (TypeError)

(包括在内,因此具有相同问题的人可能偶然发现这一点......特别是因为最后一个错误没有给出关于问题的暗示。)