Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expecting errors in rspec tests

I'm trying to expect an error in an rspec test.

lambda {Participant.create!({:user_id => three.id, :match_id => match.id, :team => 1})}.should raise_error StandardError 

For now I'm just using StandardError to make sure it's working.

1) StandardError in 'Participant should never allow more participants than players'.     This game is already full.  Cannot add another player. /home/josiah/Projects/Set-Match/app/models/participant.rb:12:in `do_not_exceed_player_count_in_match' ./spec/models/participant_spec.rb:24: 

It clearly throws the error, but my test still fails.

Thoughts?

like image 931
Josiah Kiehl Avatar asked Mar 01 '10 22:03

Josiah Kiehl


People also ask

What is allow in RSpec?

Use the allow method with the receive matcher on a test double or a real. object to tell the object to return a value (or values) in response to a given. message. Nothing happens if the message is never received.

What is subject RSpec?

Summary: RSpec's subject is a special variable that refers to the object being tested. Expectations can be set on it implicitly, which supports one-line examples. It is clear to the reader in some idiomatic cases, but is otherwise hard to understand and should be avoided.


1 Answers

Since some time, but at least in RSpec 2.5, it is possible to use

expect {raise 'boom'}.to raise_error(RuntimeError, /boom/) 
like image 65
Confusion Avatar answered Sep 21 '22 17:09

Confusion