Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't RSpec matchers working?

Tags:

rspec

rspec2

When I run this simple RSpec with any basic matcher, it doesn't work:

File: matcher_spec.rb

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

The output for any matcher used looks like this:

/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'

(yes, I'm answering my own question)

like image 341
TrinitronX Avatar asked Dec 08 '25 10:12

TrinitronX


1 Answers

This is because you forgot the 'it' block underneath the describe block. The RSpec DSL doesn't know about matchers in the scope of a describe block.

To fix it, simply add an 'it' block:

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

Symptom of problem: you see similar errors like the following

_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)

(Included so people with the same issue might stumble upon this... especially since that last error doesn't give much of a hint as to what the problem is.)

like image 118
TrinitronX Avatar answered Dec 10 '25 00:12

TrinitronX