rspec/rspec-expectations — pull request #251 by phiggins (2013-05-21 07:37:59+00:00)
Title: Allow a callable object as a custom failure message.

This pull request extends the syntax for providing a custom error message to allow a callable object (proc, lambda, etc) in place of just a string. This allows the following sort of construct:

```
1.should eq(2), "this is obviously false"
1.should eq(2), lambda { "also false" }
```

The particular use case I have for this syntax is generating the desired failure message depends on the state of the system after the matcher has run. An example would be a request spec running with capybara. If the test I'm running depends on capybara waiting until ajax requests have finished, running a spec like this would fail:

```
page.should have_css("li.widget", count: 3), page.all("li.widget").map(&:text)
```

... because the matcher is asychronous while the failure message is running synchronously. The only option I have is to do something gross like the following:

```
begin
page.should have_css("li.widget", count: 3)
rescue
$stderr.puts page.all("li.widget").map(&:text)
raise
end
```

With this pull request merged, I could instead do something like this:

```
page.should have_css("li.widget", count: 3), lambda { page.all("li.widget").map(&:text)
```

For what it's worth, [minitest has similar capabilities on its assertions](https://github.com/seattlerb/minitest/blob/master/lib/minitest/assertions.rb#L124-132).

Thanks once again for providing a great, readable, easily extendable project!