Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Would be awesome to use proc as a message #120

Closed
wants to merge 1 commit into from

2 participants

@hiukkanen

It would make possible to do things like (Rails example):

user.should(be_valid, lambda { user.errors.full_messages.inspect })
@hiukkanen hiukkanen Message can be also Proc returning string
Now it's possible to do things like (Rails example):

user.should(be_valid, lambda { user.errors.full_messages.inspect })
7bb1bc6
@dchelimsky
Owner

Thanks for the contribution, and the clean patch, but I'm not going to merge this.

I assume that the reason you need this in this case is that the matcher changes the state of the object after the failure message is generated without it. This is a very rare edge case, and is really a failure of the matcher to provide useful information (you could also argue that the matcher itself is a bad idea, since it changes the state of the object, but then you could trace that back to the fact that valid? changes the state of the object).

I also think the additional syntax would just confuse users.

@dchelimsky dchelimsky closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 9, 2012
  1. @hiukkanen

    Message can be also Proc returning string

    hiukkanen authored
    Now it's possible to do things like (Rails example):
    
    user.should(be_valid, lambda { user.errors.full_messages.inspect })
This page is out of date. Refresh to see the latest.
View
4 lib/rspec/expectations/fail_with.rb
@@ -19,6 +19,10 @@ def fail_with(message, expected=nil, actual=nil)
"appropriate failure_message_for_* method to return a string?"
end
+ if message.is_a? Proc
+ message = message.call
+ end
+
if actual && expected
if all_strings?(actual, expected)
if any_multiline_strings?(actual, expected)
View
6 spec/rspec/expectations/fail_with_spec.rb
@@ -66,5 +66,11 @@
RSpec::Expectations.fail_with "the message", lambda {}, lambda {}
}.should fail_with("the message")
end
+
+ it "accept message as Proc" do
+ lambda {
+ RSpec::Expectations.fail_with lambda { "the message"}
+ }.should fail_with("the message")
+ end
end
Something went wrong with that request. Please try again.