8

Learning how to Rspec 3. I have a question on the matchers. The tutorial i am following is based on Rspec 2.

describe Team do

  it "has a name" do
    #Team.new("Random name").should respond_to :name
    expect { Team.new("Random name") }.to be(:name)
  end


  it "has a list of players" do
    #Team.new("Random name").players.should be_kind_of Array
    expect { Team.new("Random name").players }.to be_kind_of(Array)
  end

end

Why is the code causing an error while the one i commented out passing with depreciation warning.

Error

Failures:

  1) Team has a name
     Failure/Error: expect { Team.new("Random name") }.to be(:name)
       You must pass an argument rather than a block to use the provided matcher (equal :name), or the matcher must implement `supports_block_expectations?`.
     # ./spec/team_spec.rb:7:in `block (2 levels) in <top (required)>'

  2) Team has a list of players
     Failure/Error: expect { Team.new("Random name").players }.to be_kind_of(Array)
       You must pass an argument rather than a block to use the provided matcher (be a kind of Array), or the matcher must implement `supports_block_expectations?`.
     # ./spec/team_spec.rb:13:in `block (2 levels) in <top (required)>'
eugen
  • 8,916
  • 11
  • 57
  • 65
Benjamin
  • 2,108
  • 2
  • 26
  • 46
  • 1
    Check this [answer](http://stackoverflow.com/questions/19960831/rspec-expect-vs-expect-with-block-whats-the-difference) for _why?_ – Arup Rakshit Sep 30 '14 at 10:05

1 Answers1

11

You should use normal brackets for those tests:

expect(Team.new("Random name")).to eq :name

When you use curly brackets, you are passing a block of code. For rspec3 it means that you will put some expectations about the execution of this block rather than on the result of execution, so for example

expect { raise 'hello' }.to raise_error

EDIT:

Note however that this test will fail, as Team.new returns an object, not a symbol. You can modify your test so it passes:

expect(Team.new("Random name")).to respond_to :name

# or

expect(Team.new("Random name").name).to eq "Random name"
BroiSatse
  • 44,031
  • 8
  • 61
  • 86
  • I get an error with this. https://gist.github.com/vezu/85661922adda6a877b48 . Thank you for the explanation. – Benjamin Sep 30 '14 at 10:07
  • 1
    @Benjamin - I would say it is expected, as `Team.new` returns an object, not a symbol. Answer updated. – BroiSatse Sep 30 '14 at 10:11