From 7bb1bc66ae216a752b8e0cfad9084a9e120a81ca Mon Sep 17 00:00:00 2001 From: Joni Lahtinen Date: Fri, 9 Mar 2012 21:04:11 +0200 Subject: [PATCH] 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 }) --- lib/rspec/expectations/fail_with.rb | 4 ++++ spec/rspec/expectations/fail_with_spec.rb | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/lib/rspec/expectations/fail_with.rb b/lib/rspec/expectations/fail_with.rb index edca8224b..08c9f72eb 100644 --- a/lib/rspec/expectations/fail_with.rb +++ b/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) diff --git a/spec/rspec/expectations/fail_with_spec.rb b/spec/rspec/expectations/fail_with_spec.rb index 84646e27e..bc4837fcd 100644 --- a/spec/rspec/expectations/fail_with_spec.rb +++ b/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