Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

Test::Unit or MiniTest).

- Closes #297.
  • Loading branch information...
commit 7887b14532b03fac92b44cd0785d557f39144143 1 parent 026fb86
@dchelimsky dchelimsky authored
View
3  History.md
@@ -10,6 +10,9 @@
* Bug fixes
* restore global config.render_views so you only need to say it once
* support overriding render_views in nested groups
+ * matchers that delegate to Rails' assertions capture
+ ActiveSupport::TestCase::Assertion (so they work properly now with
+ should_not in Ruby 1.8.7 and 1.9.1)
### 2.3.1 / 2010-12-16
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
6 lib/rspec/rails/matchers/render_template.rb
@@ -3,7 +3,7 @@ module RenderTemplate
extend RSpec::Matchers::DSL
matcher :render_template do |options, message|
- match_unless_raises Test::Unit::AssertionFailedError do |_|
+ match_unless_raises ActiveSupport::TestCase::Assertion do |_|
options = options.to_s if Symbol === options
assert_template options, message
end
@@ -11,6 +11,10 @@ module RenderTemplate
failure_message_for_should do
rescued_exception.message
end
+
+ failure_message_for_should_not do |_|
+ "expected not to render #{options.inspect}, but did"
+ end
end
end
end
View
2  lib/rspec/rails/matchers/routing_matchers.rb
@@ -3,7 +3,7 @@ module RoutingMatchers
extend RSpec::Matchers::DSL
matcher :route_to do |route_options|
- match_unless_raises Test::Unit::AssertionFailedError do |path|
+ match_unless_raises ActiveSupport::TestCase::Assertion do |path|
assertion_path = { :method => path.keys.first, :path => path.values.first }
assert_recognizes(route_options, assertion_path)
end
View
41 spec/rspec/rails/matchers/be_routable_spec.rb
@@ -0,0 +1,41 @@
+require "spec_helper"
+
+describe "be_routable" do
+ include RSpec::Rails::Matchers::RoutingMatchers
+ attr_reader :routes
+
+ before { @routes = double("routes") }
+
+ context "with should" do
+ it "passes if routes recognize the path" do
+ routes.stub(:recognize_path) { {} }
+ expect do
+ {:get => "/a/path"}.should be_routable
+ end.to_not raise_error
+ end
+
+ it "fails if routes do not recognize the path" do
+ routes.stub(:recognize_path) { raise ActionController::RoutingError.new('ignore') }
+ expect do
+ {:get => "/a/path"}.should be_routable
+ end.to raise_error(/expected \{:get=>"\/a\/path"\} to be routable/)
+ end
+ end
+
+ context "with should_not" do
+
+ it "passes if routes do not recognize the path" do
+ routes.stub(:recognize_path) { raise ActionController::RoutingError.new('ignore') }
+ expect do
+ {:get => "/a/path"}.should_not be_routable
+ end.to_not raise_error
+ end
+
+ it "fails if routes recognize the path" do
+ routes.stub(:recognize_path) { {:controller => "foo"} }
+ expect do
+ {:get => "/a/path"}.should_not be_routable
+ end.to raise_error(/expected \{:get=>"\/a\/path"\} not to be routable, but it routes to \{:controller=>"foo"\}/)
+ end
+ end
+end
View
75 spec/rspec/rails/matchers/redirect_to_spec.rb
@@ -1,20 +1,79 @@
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 "when assert_redirected_to passes" do
+ it "passes" do
+ self.stub!(:assert_redirected_to)
+ expect do
+ response.should redirect_to("destination")
+ end.to_not raise_exception
+ end
+ end
+
+ context "when assert_redirected_to fails" 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 "when fails 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 "when 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 "when assert_redirected_to passes" do
+ it "fails with custom failure message" 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
+
+ context "when fails due to some other exception" do
+ it "raises that exception" do
+ self.stub!(:assert_redirected_to) do
+ raise "oops"
+ end
+ expect do
+ response.should_not redirect_to("destination")
+ end.to raise_exception("oops")
+ end
+ end
end
end
View
77 spec/rspec/rails/matchers/render_template_spec.rb
@@ -2,15 +2,7 @@
describe "render_template" do
include RSpec::Rails::Matchers::RenderTemplate
-
- 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
+ let(:response) { ActionController::TestResponse.new }
context "given a hash" do
it "delegates to assert_template" do
@@ -32,5 +24,70 @@
"response".should render_template(:template_name, "this message")
end
end
-end
+ context "with should" do
+ context "when assert_template passes" do
+ it "passes" do
+ self.stub!(:assert_template)
+ expect do
+ response.should render_template("template_name")
+ end.to_not raise_exception
+ end
+ end
+
+ context "when assert_template fails" do
+ it "uses failure message from assert_template" do
+ self.stub!(:assert_template) do
+ raise ActiveSupport::TestCase::Assertion.new("this message")
+ end
+ expect do
+ response.should render_template("template_name")
+ end.to raise_error("this message")
+ end
+ end
+
+ context "when fails due to some other exception" do
+ it "raises that exception" do
+ self.stub!(:assert_template) do
+ raise "oops"
+ end
+ expect do
+ response.should render_template("template_name")
+ end.to raise_exception("oops")
+ end
+ end
+ end
+
+ context "with should_not" do
+ context "when assert_template fails" do
+ it "passes" do
+ self.stub!(:assert_template) do
+ raise ActiveSupport::TestCase::Assertion.new("this message")
+ end
+ expect do
+ response.should_not render_template("template_name")
+ end.to_not raise_exception
+ end
+ end
+
+ context "when assert_template passes" do
+ it "fails with custom failure message" do
+ self.stub!(:assert_template)
+ expect do
+ response.should_not render_template("template_name")
+ end.to raise_error(/expected not to render \"template_name\", but did/)
+ end
+ end
+
+ context "when fails due to some other exception" do
+ it "raises that exception" do
+ self.stub!(:assert_template) do
+ raise "oops"
+ end
+ expect do
+ response.should_not render_template("template_name")
+ end.to raise_exception("oops")
+ end
+ end
+ end
+end
View
97 spec/rspec/rails/matchers/route_to_spec.rb
@@ -3,51 +3,82 @@
describe "route_to" do
include RSpec::Rails::Matchers::RoutingMatchers
- it "uses failure message from assert_recognizes" do
- self.stub!(:assert_recognizes).and_raise(
- Test::Unit::AssertionFailedError.new("this message"))
- expect do
- {"this" => "path"}.should route_to("these" => "options")
- end.to raise_error("this message")
+ it "delegates to assert_recognizes" do
+ self.should_receive(:assert_recognizes).with({ "these" => "options" }, { :method=> :get, :path=>"path" })
+ {:get => "path"}.should route_to("these" => "options")
end
-end
-
-describe "be_routable" do
- include RSpec::Rails::Matchers::RoutingMatchers
- attr_reader :routes
-
- before { @routes = double("routes") }
context "with should" do
- it "passes if routes recognize the path" do
- routes.stub(:recognize_path) { {} }
- expect do
- {:get => "/a/path"}.should be_routable
- end.to_not raise_error
+ context "when assert_recognizes passes" do
+ it "passes" do
+ self.stub!(:assert_recognizes)
+ expect do
+ {:get => "path"}.should route_to("these" => "options")
+ end.to_not raise_exception
+ end
end
- it "fails if routes do not recognize the path" do
- routes.stub(:recognize_path) { raise ActionController::RoutingError.new('ignore') }
- expect do
- {:get => "/a/path"}.should be_routable
- end.to raise_error(/expected \{:get=>"\/a\/path"\} to be routable/)
+ context "when assert_recognizes fails" do
+ it "fails with message from assert_recognizes" do
+ self.stub!(:assert_recognizes) do
+ raise ActiveSupport::TestCase::Assertion.new("this message")
+ end
+ expect do
+ {:get => "path"}.should route_to("these" => "options")
+ end.to raise_error("this message")
+ end
+ end
+
+ context "when an exception is raised" do
+ it "raises that exception" do
+ self.stub!(:assert_recognizes) do
+ raise "oops"
+ end
+ expect do
+ {:get => "path"}.should route_to("these" => "options")
+ end.to raise_exception("oops")
+ end
end
end
context "with should_not" do
+ context "when assert_recognizes passes" do
+ it "fails with custom message" do
+ self.stub!(:assert_recognizes)
+ expect do
+ {:get => "path"}.should_not route_to("these" => "options")
+ end.to raise_error(/expected .* not to route to .*/)
+ end
+ end
- it "passes if routes do not recognize the path" do
- routes.stub(:recognize_path) { raise ActionController::RoutingError.new('ignore') }
- expect do
- {:get => "/a/path"}.should_not be_routable
- end.to_not raise_error
+ context "when assert_recognizes fails" do
+ it "passes" do
+ self.stub!(:assert_recognizes) do
+ raise ActiveSupport::TestCase::Assertion.new("this message")
+ end
+ expect do
+ {:get => "path"}.should_not route_to("these" => "options")
+ end.to_not raise_error
+ end
end
- it "fails if routes recognize the path" do
- routes.stub(:recognize_path) { {:controller => "foo"} }
- expect do
- {:get => "/a/path"}.should_not be_routable
- end.to raise_error(/expected \{:get=>"\/a\/path"\} not to be routable, but it routes to \{:controller=>"foo"\}/)
+ context "when an exception is raised" do
+ it "raises that exception" do
+ self.stub!(:assert_recognizes) do
+ raise "oops"
+ end
+ expect do
+ {:get => "path"}.should_not route_to("these" => "options")
+ end.to raise_exception("oops")
+ end
end
end
+ it "uses failure message from assert_recognizes" do
+ self.stub!(:assert_recognizes).and_raise(
+ ActiveSupport::TestCase::Assertion.new("this message"))
+ expect do
+ {"this" => "path"}.should route_to("these" => "options")
+ end.to raise_error("this message")
+ end
end
+
Please sign in to comment.
Something went wrong with that request. Please try again.