[rspec-users] syntax for custom messages

Justin Ko jko170 at gmail.com
Mon Mar 5 15:13:57 UTC 2012


On Mar 5, 2012, at 1:18 AM, ruud144 wrote:

> hi group,
> 
> I read that expectations can print a custom message on failure using a
> syntax like
> 
> cars.should be_empty, "Cars left"

This is because RSpec predicate matchers can accept a block:
https://github.com/rspec/rspec-expectations/blob/master/spec/rspec/matchers/be_spec.rb#L174

> 
> But when I try this syntax for this expectation:
> 
> string.should == 'Cars left', 'Yippee, no cars anymore'

This will work:
string.should eq('Cars left'), 'Yippee, no cars anymore'

> 
> I get a syntax error:
> 
> syntax error, unexpected ',', expecting keyword_end (SyntaxError)
> 
> I want two things:
> - I want a syntax error free expectation for should ==
> - I want to understand what the mechanism is. I am afraid that my ruby
> knowledge is not sufficient. Clearly, should is a function, but what
> are be_empty and == then? Parameters? Can I use parentheses?
> 
> Thanks for helping me!
> 
> Ruud
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Personally, I think custom messages should be deprecated. Nobody uses them and it's impossible to support them 100% of the time (as in predicate matchers). The default failure messages work 99% of the time, and if it doesn't, a custom matcher is the way to go.

David?



More information about the rspec-users mailing list