RSpec 3 vs RSpec 2

Learning how to Rspec 3. I have a question about helpers. 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 the code causes an error, while one of them commented on the passage with a warning about depreciation.

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)>' 
+5
source share
1 answer

For these tests, you should use regular brackets:

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

When you use curly braces, you pass a block of code. For rspec3, this means that you will rely on the execution of this block and not on the result of the execution, for example,

 expect { raise 'hello' }.to raise_error 

EDIT:

Note that this test fails because Team.new returns an object, not a character. You can change your test to pass:

 expect(Team.new("Random name")).to respond_to :name # or expect(Team.new("Random name").name).to eq "Random name" 
+6
source

Source: https://habr.com/ru/post/1203697/


All Articles