Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which style,lambda..should or expect..to, is preferred for testing expectations in RSpec?

Tags:

ruby

rspec

rspec2

I have seen both styles used widely: #1 lambda { raise "Boom" }.should raise_error and #2 expect { raise "Boom" }.to raise_error. I like expect..to more as it reads better and hides the creation of the proc.

I looked at rspec code and it seems expect..to is suggested, however I regularly come across libraries using lambda..should. Is expect..to newer and hence not "famous" yet?

like image 692
arnab Avatar asked Nov 16 '10 03:11

arnab


1 Answers

expect is used since rspec-2, previously lambda had to be used.

RSpec "officially" recommends to use expect and it is possible that they will decide to "obsolete" lambda syntax.

The lambda syntax is used in most of the libraries that started life in RSpec1 days. They just haven't yet migrated (and why would they if it is still supported).

So, do use expect instead of lambda.

like image 126
Dmytrii Nagirniak Avatar answered Sep 19 '22 03:09

Dmytrii Nagirniak