Permalink
Browse files

Fixed should_fail to work with matchers; ActionController macros use …

…matchers
  • Loading branch information...
jferris committed Apr 21, 2010
1 parent f1f371e commit 73a074f30b43a280e70dcbe9c793581aa03ebffb
Showing with 43 additions and 72 deletions.
  1. +15 −64 lib/shoulda/action_controller/macros.rb
  2. +8 −4 lib/shoulda/context.rb
  3. +20 −4 test/fail_macros.rb
@@ -32,18 +32,12 @@ module Macros
# should_set_the_flash_to "Thank you for placing this order."
# should_set_the_flash_to /created/i
def should_set_the_flash_to(val)
- matcher = set_the_flash.to(val)
- should matcher.description do
- assert_accepts matcher, @controller
- end
+ should set_the_flash.to(val)
end
# Macro that creates a test asserting that the flash is empty.
def should_not_set_the_flash
- matcher = set_the_flash
- should "not #{matcher.description}" do
- assert_rejects matcher, @controller
- end
+ should_not set_the_flash
end
# Macro that creates a test asserting that filter_parameter_logging
@@ -54,10 +48,7 @@ def should_not_set_the_flash
# should_filter_params :password, :ssn
def should_filter_params(*keys)
keys.each do |key|
- matcher = filter_param(key)
- should matcher.description do
- assert_accepts matcher, @controller
- end
+ should filter_param(key)
end
end
@@ -79,14 +70,8 @@ def should_assign_to(*names, &block)
klass = get_options!(names, :class)
names.each do |name|
matcher = assign_to(name).with_kind_of(klass)
- should matcher.description do
- if block
- expected_value = instance_eval(&block)
- matcher = matcher.with(expected_value)
- end
-
- assert_accepts matcher, @controller
- end
+ matcher = matcher.with(&block) if block
+ should matcher
end
end
@@ -98,10 +83,7 @@ def should_assign_to(*names, &block)
# should_not_assign_to :user, :posts
def should_not_assign_to(*names)
names.each do |name|
- matcher = assign_to(name)
- should "not #{matcher.description}" do
- assert_rejects matcher, @controller
- end
+ should_not assign_to(name)
end
end
@@ -110,10 +92,7 @@ def should_not_assign_to(*names)
#
# should_respond_with :success
def should_respond_with(response)
- should "respond with #{response}" do
- matcher = respond_with(response)
- assert_accepts matcher, @controller
- end
+ should respond_with(response)
end
# Macro that creates a test asserting that the response content type was 'content_type'.
@@ -123,10 +102,7 @@ def should_respond_with(response)
# should_respond_with_content_type :rss
# should_respond_with_content_type /rss/
def should_respond_with_content_type(content_type)
- matcher = respond_with_content_type(content_type)
- should matcher.description do
- assert_accepts matcher, @controller
- end
+ should respond_with_content_type(content_type)
end
# Macro that creates a test asserting that a value returned from the
@@ -139,44 +115,30 @@ def should_respond_with_content_type(content_type)
# should_set_session(:message) { "Free stuff" }
def should_set_session(key, &block)
matcher = set_session(key)
- should matcher.description do
- expected_value = instance_eval(&block)
- matcher = matcher.to(expected_value)
- assert_accepts matcher, @controller
- end
+ matcher = matcher.to(&block) if block
+ should matcher
end
# Macro that creates a test asserting that the controller rendered the given template.
# Example:
#
# should_render_template :new
def should_render_template(template)
- should "render template #{template.inspect}" do
- assert_template template.to_s
- end
+ should render_template(template)
end
# Macro that creates a test asserting that the controller rendered with the given layout.
# Example:
#
# should_render_with_layout 'special'
def should_render_with_layout(expected_layout = 'application')
- matcher = render_with_layout(expected_layout)
- if expected_layout
- should matcher.description do
- assert_accepts matcher, @controller
- end
- else
- should "render without layout" do
- assert_rejects matcher, @controller
- end
- end
+ should render_with_layout(expected_layout)
end
# Macro that creates a test asserting that the controller rendered without a layout.
# Same as @should_render_with_layout false@
def should_render_without_layout
- should_render_with_layout nil
+ should_not render_with_layout
end
# Macro that creates a test asserting that the controller returned a
@@ -188,10 +150,7 @@ def should_render_without_layout
#
# should_redirect_to("the user's profile") { user_url(@user) }
def should_redirect_to(description, &block)
- should "redirect to #{description}" do
- expected_url = instance_eval(&block)
- assert_redirected_to expected_url
- end
+ should redirect_to(description, &block)
end
# Macro that creates a routing test. It tries to use the given HTTP
@@ -216,15 +175,7 @@ def should_redirect_to(description, &block)
# :action => :show, :id => 1, :user_id => 1
#
def should_route(method, path, options)
- unless options[:controller]
- options[:controller] = self.name.gsub(/ControllerTest$/, '').tableize
- end
-
- matcher = route(method, path).to(options)
-
- should matcher.description do
- assert_accepts matcher.in_context(self), self
- end
+ should route(method, path).to(options)
end
end
end
View
@@ -88,11 +88,15 @@ def should(name_or_matcher, options = {}, &blk)
#
# should_not set_the_flash
def should_not(matcher)
- context_name = self.name.gsub(/Test/, "")
- context = Shoulda::Context.new(context_name, self) do
- should_not(matcher)
+ if Shoulda.current_context
+ Shoulda.current_context.should_not(matcher)
+ else
+ context_name = self.name.gsub(/Test/, "")
+ context = Shoulda::Context.new(context_name, self) do
+ should_not(matcher)
+ end
+ context.build
end
- context.build
end
# == Before statements
View
@@ -27,13 +27,29 @@ def should_fail(&block)
class Context
# alias_method_chain hack to allow the should_fail macro to work
- def should_with_failure_scenario(name, options = {}, &block)
+ def should_with_failure_scenario(*args, &block)
+ should_without_failure_scenario(*args, &block)
+ wrap_last_should_with_failure_expectation
+ end
+ alias_method_chain :should, :failure_scenario
+
+ # alias_method_chain hack to allow the should_fail macro to work
+ def should_not_with_failure_scenario(*args, &block)
+ should_not_without_failure_scenario(*args, &block)
+ wrap_last_should_with_failure_expectation
+ end
+ alias_method_chain :should_not, :failure_scenario
+
+ def wrap_last_should_with_failure_expectation
if Shoulda.expected_exceptions
expected_exceptions = Shoulda.expected_exceptions
- failure_block = lambda { assert_raise(*expected_exceptions, &block.bind(self)) }
+ should = self.shoulds.last
+ assertion_block = should[:block]
+ failure_block = lambda do
+ assert_raise(*expected_exceptions, &assertion_block.bind(self))
+ end
+ should[:block] = failure_block
end
- should_without_failure_scenario(name, options, &(failure_block || block))
end
- alias_method_chain :should, :failure_scenario
end
end

0 comments on commit 73a074f

Please sign in to comment.