Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use ActiveSupport::TestCase::Assertion (which knows whether we're using

Test::Unit or MiniTest.
  • Loading branch information...
commit 70dce6b632d7fbedf589b42a1bf1dbf72f5065d0 1 parent 713e4d8
@dchelimsky dchelimsky authored
View
8 lib/rspec/rails/matchers/redirect_to.rb
@@ -3,13 +3,17 @@ module RedirectTo
extend RSpec::Matchers::DSL
matcher :redirect_to do |destination|
- match_unless_raises Test::Unit::AssertionFailedError do |_|
+ match_unless_raises ActiveSupport::TestCase::Assertion do |_|
assert_redirected_to destination
end
- failure_message_for_should do
+ failure_message_for_should do |_|
rescued_exception.message
end
+
+ failure_message_for_should_not do |_|
+ "expected not to redirect to #{destination.inspect}, but did"
+ end
end
end
end
View
64 spec/rspec/rails/matchers/redirect_to_spec.rb
@@ -1,20 +1,68 @@
require "spec_helper"
-require "action_controller/test_case"
+require "active_support/test_case"
describe "redirect_to" do
include RSpec::Rails::Matchers::RedirectTo
+ let(:response) { ActionController::TestResponse.new }
+
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")
+ context "with should" do
+ context "passing" do
+ it "passes" do
+ self.stub!(:assert_redirected_to)
+ expect do
+ response.should redirect_to("destination")
+ end.to_not raise_exception
+ end
+ end
+
+ context "failing due to assert_redirected_to failing" do
+ it "uses failure message from assert_redirected_to" do
+ self.stub!(:assert_redirected_to) do
+ raise ActiveSupport::TestCase::Assertion.new("this message")
+ end
+ expect do
+ response.should redirect_to("destination")
+ end.to raise_error("this message")
+ end
+ end
+
+ context "failing due to some other exception" do
+ it "raises that exception" do
+ self.stub!(:assert_redirected_to) do
+ raise "oops"
+ end
+ expect do
+ response.should redirect_to("destination")
+ end.to raise_exception("oops")
+ end
+ end
+ end
+
+ context "with should_not" do
+ context "passing (i.e. assert_redirected_to fails)" do
+ it "passes" do
+ self.stub!(:assert_redirected_to) do
+ raise ActiveSupport::TestCase::Assertion.new("this message")
+ end
+ expect do
+ response.should_not redirect_to("destination")
+ end.to_not raise_exception
+ end
+ end
+
+ context "failing due to assert_redirected_to passing" do
+ it "uses failure message from assert_redirected_to" do
+ self.stub!(:assert_redirected_to)
+ expect do
+ response.should_not redirect_to("destination")
+ end.to raise_error(/expected not to redirect to \"destination\", but did/)
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.