From d29bbf5a06d20ec23577a7ff747f2a17f685803d Mon Sep 17 00:00:00 2001 From: David Chelimsky Date: Mon, 7 Jun 2010 14:47:53 -0400 Subject: [PATCH] use failure messages from rails assertions when delegating to them --- lib/rspec/rails/matchers.rb | 8 ++++++++ spec/rspec/rails/matchers/redirect_to_spec.rb | 10 ++++++++++ spec/rspec/rails/matchers/render_template_spec.rb | 9 +++++++++ 3 files changed, 27 insertions(+) diff --git a/lib/rspec/rails/matchers.rb b/lib/rspec/rails/matchers.rb index abfd939726..108707b840 100644 --- a/lib/rspec/rails/matchers.rb +++ b/lib/rspec/rails/matchers.rb @@ -23,6 +23,10 @@ class AssertionFailedError < StandardError match_unless_raises Test::Unit::AssertionFailedError do |_| assert_redirected_to destination end + + failure_message_for_should do + rescued_exception.message + end end RSpec::Matchers.define :render_template do |options, message| @@ -30,6 +34,10 @@ class AssertionFailedError < StandardError options = options.to_s if Symbol === options assert_template options, message end + + failure_message_for_should do + rescued_exception.message + end end RSpec::Matchers.define :be_a_new do |model_klass| diff --git a/spec/rspec/rails/matchers/redirect_to_spec.rb b/spec/rspec/rails/matchers/redirect_to_spec.rb index fefe3b8e1f..e6ffe4a685 100644 --- a/spec/rspec/rails/matchers/redirect_to_spec.rb +++ b/spec/rspec/rails/matchers/redirect_to_spec.rb @@ -1,8 +1,18 @@ require "spec_helper" +require "action_controller/test_case" describe "redirect_to" do it "delegates to assert_redirected_to" do self.should_receive(:assert_redirected_to).with("destination") "response".should redirect_to("destination") end + + it "uses failure message from assert_redirected_to" do + self.stub!(:assert_redirected_to).and_raise( + Test::Unit::AssertionFailedError.new("this message")) + response = ActionController::TestResponse.new + expect do + response.should redirect_to("destination") + end.to raise_error("this message") + end end diff --git a/spec/rspec/rails/matchers/render_template_spec.rb b/spec/rspec/rails/matchers/render_template_spec.rb index e29de88b33..8da4b57581 100644 --- a/spec/rspec/rails/matchers/render_template_spec.rb +++ b/spec/rspec/rails/matchers/render_template_spec.rb @@ -1,6 +1,15 @@ require "spec_helper" describe "render_template" do + it "uses failure message from render_template" do + self.stub!(:assert_template).and_raise( + Test::Unit::AssertionFailedError.new("this message")) + response = ActionController::TestResponse.new + expect do + response.should render_template("destination") + end.to raise_error("this message") + end + context "given a hash" do it "delegates to assert_template" do self.should_receive(:assert_template).with({:this => "hash"}, "this message")