[rspec-users] catching errors, rspec basics
Ben Mabey
ben at benmabey.com
Tue Jan 29 22:58:30 EST 2008
Andrew WC Brown wrote:
> ohhhhh, I left in:
>
> it "should return an error when passed a string" do
> @audience.stats = 'Market Goblin'
> lambda {@audience.stats = 'Market Goblin'}.should raise_error
> end
>
> when yours is:
>
> it "should return an error when passed a string" do
> lambda {@audience.stats = 'Market Goblin'}.should raise_error
> end
>
> I've seen lambda before but not sure what it does.
A lambda in ruby is like a block or a Proc. There are some differences
between them, but for this simple use you can just think of it as a
block of code that will be passed to the 'raise_error' matcher that then
runs that block of code checking to see if an Exception is raised when
it is ran. You can also pass in the specific exception type to be more
specific.
As a personal preference I like to alias lambda to 'running' in my
spec_helper.rb... I think this reads a lot better:
running {@audience.stats = 'Market Goblin'}.should raise_error
For more on lambdas refer to the pickaxe. If you want more detailed
information of the subtle differences between them, Procs, blocks, and
methods check this out:
http://innig.net/software/ruby/closures-in-ruby.rb
-Ben
More information about the rspec-users
mailing list