Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Removed the Matchers namespace

  • Loading branch information...
commit e7eee1a6b7f9dd1862ca05ec470b792d86c37ebf 1 parent 11d7dca
@jferris jferris authored jferris committed
Showing with 1,825 additions and 1,882 deletions.
  1. +23 −26 lib/shoulda/action_controller.rb
  2. +89 −91 lib/shoulda/action_controller/assign_to_matcher.rb
  3. +32 −34 lib/shoulda/action_controller/filter_param_matcher.rb
  4. +45 −47 lib/shoulda/action_controller/redirect_to_matcher.rb
  5. +37 −39 lib/shoulda/action_controller/render_template_matcher.rb
  6. +70 −72 lib/shoulda/action_controller/render_with_layout_matcher.rb
  7. +52 −54 lib/shoulda/action_controller/respond_with_content_type_matcher.rb
  8. +60 −62 lib/shoulda/action_controller/respond_with_matcher.rb
  9. +69 −71 lib/shoulda/action_controller/route_matcher.rb
  10. +71 −73 lib/shoulda/action_controller/set_session_matcher.rb
  11. +69 −71 lib/shoulda/action_controller/set_the_flash_matcher.rb
  12. +16 −18 lib/shoulda/action_mailer.rb
  13. +81 −83 lib/shoulda/action_mailer/have_sent_email.rb
  14. +21 −23 lib/shoulda/active_record.rb
  15. +56 −58 lib/shoulda/active_record/allow_mass_assignment_of_matcher.rb
  16. +82 −84 lib/shoulda/active_record/allow_value_matcher.rb
  17. +183 −185 lib/shoulda/active_record/association_matcher.rb
  18. +63 −65 lib/shoulda/active_record/ensure_inclusion_of_matcher.rb
  19. +110 −112 lib/shoulda/active_record/ensure_length_of_matcher.rb
  20. +136 −138 lib/shoulda/active_record/have_db_column_matcher.rb
  21. +85 −87 lib/shoulda/active_record/have_db_index_matcher.rb
  22. +39 −41 lib/shoulda/active_record/have_readonly_attribute_matcher.rb
  23. +27 −29 lib/shoulda/active_record/validate_acceptance_of_matcher.rb
  24. +48 −50 lib/shoulda/active_record/validate_format_of_matcher.rb
  25. +26 −28 lib/shoulda/active_record/validate_numericality_of_matcher.rb
  26. +43 −45 lib/shoulda/active_record/validate_presence_of_matcher.rb
  27. +119 −121 lib/shoulda/active_record/validate_uniqueness_of_matcher.rb
  28. +38 −40 lib/shoulda/active_record/validation_matcher.rb
  29. +3 −3 lib/shoulda/integrations/rspec.rb
  30. +6 −6 lib/shoulda/integrations/test_unit.rb
  31. +1 −1  spec/shoulda/action_controller/assign_to_matcher_spec.rb
  32. +1 −1  spec/shoulda/action_controller/filter_param_matcher_spec.rb
  33. +1 −1  spec/shoulda/action_controller/redirect_to_matcher_spec.rb
  34. +1 −1  spec/shoulda/action_controller/render_template_matcher_spec.rb
  35. +1 −1  spec/shoulda/action_controller/render_with_layout_matcher_spec.rb
  36. +1 −1  spec/shoulda/action_controller/respond_with_content_type_matcher_spec.rb
  37. +1 −1  spec/shoulda/action_controller/respond_with_matcher_spec.rb
  38. +1 −1  spec/shoulda/action_controller/route_matcher_spec.rb
  39. +1 −1  spec/shoulda/action_controller/set_session_matcher_spec.rb
  40. +1 −1  spec/shoulda/action_controller/set_the_flash_matcher_spec.rb
  41. +1 −1  spec/shoulda/action_mailer/have_sent_email_spec.rb
  42. +1 −1  spec/shoulda/active_record/allow_mass_assignment_of_matcher_spec.rb
  43. +1 −1  spec/shoulda/active_record/allow_value_matcher_spec.rb
  44. +1 −1  spec/shoulda/active_record/association_matcher_spec.rb
  45. +1 −1  spec/shoulda/active_record/ensure_inclusion_of_matcher_spec.rb
  46. +1 −1  spec/shoulda/active_record/ensure_length_of_matcher_spec.rb
  47. +1 −1  spec/shoulda/active_record/have_db_column_matcher_spec.rb
  48. +1 −1  spec/shoulda/active_record/have_db_index_matcher_spec.rb
  49. +1 −1  spec/shoulda/active_record/have_readonly_attributes_matcher_spec.rb
  50. +1 −1  spec/shoulda/active_record/validate_acceptance_of_matcher_spec.rb
  51. +1 −1  spec/shoulda/active_record/validate_format_of_matcher_spec.rb
  52. +1 −1  spec/shoulda/active_record/validate_numericality_of_matcher_spec.rb
  53. +1 −1  spec/shoulda/active_record/validate_presence_of_matcher_spec.rb
  54. +1 −1  spec/shoulda/active_record/validate_uniqueness_of_matcher_spec.rb
  55. +2 −2 spec/spec_helper.rb
View
49 lib/shoulda/action_controller.rb
@@ -9,31 +9,28 @@
require 'shoulda/action_controller/redirect_to_matcher'
require 'shoulda/action_controller/render_template_matcher'
-module Shoulda # :nodoc:
- module ActionController # :nodoc:
-
- # By using the matchers you can quickly and easily create concise and
- # easy to read test suites.
- #
- # This code segment:
- #
- # describe UsersController, "on GET to show with a valid id" do
- # before(:each) do
- # get :show, :id => User.first.to_param
- # end
- #
- # it { should assign_to(:user) }
- # it { should respond_with(:success) }
- # it { should render_template(:show) }
- # it { should not_set_the_flash) }
- #
- # it "should do something else really cool" do
- # assigns[:user].id.should == 1
- # end
- # end
- #
- # Would produce 5 tests for the show action
- module Matchers
- end
+module Shoulda
+ # By using the matchers you can quickly and easily create concise and
+ # easy to read test suites.
+ #
+ # This code segment:
+ #
+ # describe UsersController, "on GET to show with a valid id" do
+ # before(:each) do
+ # get :show, :id => User.first.to_param
+ # end
+ #
+ # it { should assign_to(:user) }
+ # it { should respond_with(:success) }
+ # it { should render_template(:show) }
+ # it { should not_set_the_flash) }
+ #
+ # it "should do something else really cool" do
+ # assigns[:user].id.should == 1
+ # end
+ # end
+ #
+ # Would produce 5 tests for the show action
+ module ActionController
end
end
View
180 lib/shoulda/action_controller/assign_to_matcher.rb
@@ -1,114 +1,112 @@
module Shoulda # :nodoc:
module ActionController # :nodoc:
- module Matchers
-
- # Ensures that the controller assigned to the named instance variable.
- #
- # Options:
- # * <tt>with_kind_of</tt> - The expected class of the instance variable
- # being checked.
- # * <tt>with</tt> - The value that should be assigned.
- #
- # Example:
- #
- # it { should assign_to(:user) }
- # it { should_not assign_to(:user) }
- # it { should assign_to(:user).with_kind_of(User) }
- # it { should assign_to(:user).with(@user) }
- def assign_to(variable)
- AssignToMatcher.new(variable)
- end
- class AssignToMatcher # :nodoc:
+ # Ensures that the controller assigned to the named instance variable.
+ #
+ # Options:
+ # * <tt>with_kind_of</tt> - The expected class of the instance variable
+ # being checked.
+ # * <tt>with</tt> - The value that should be assigned.
+ #
+ # Example:
+ #
+ # it { should assign_to(:user) }
+ # it { should_not assign_to(:user) }
+ # it { should assign_to(:user).with_kind_of(User) }
+ # it { should assign_to(:user).with(@user) }
+ def assign_to(variable)
+ AssignToMatcher.new(variable)
+ end
- def initialize(variable)
- @variable = variable.to_s
- @check_value = false
- end
+ class AssignToMatcher # :nodoc:
- def with_kind_of(expected_class)
- @expected_class = expected_class
- self
- end
+ def initialize(variable)
+ @variable = variable.to_s
+ @check_value = false
+ end
- def with(expected_value = nil, &block)
- @check_value = true
- @expected_value = expected_value
- @expectation_block = block
- self
- end
+ def with_kind_of(expected_class)
+ @expected_class = expected_class
+ self
+ end
- def matches?(controller)
- @controller = controller
- @expected_value = @context.instance_eval(&@expectation_block) if @expectation_block
- assigned_value? && kind_of_expected_class? && equal_to_expected_value?
- end
+ def with(expected_value = nil, &block)
+ @check_value = true
+ @expected_value = expected_value
+ @expectation_block = block
+ self
+ end
- attr_reader :failure_message, :negative_failure_message
+ def matches?(controller)
+ @controller = controller
+ @expected_value = @context.instance_eval(&@expectation_block) if @expectation_block
+ assigned_value? && kind_of_expected_class? && equal_to_expected_value?
+ end
- def description
- description = "assign @#{@variable}"
- description << " with a kind of #{@expected_class}" if @expected_class
- description
- end
+ attr_reader :failure_message, :negative_failure_message
- def in_context(context)
- @context = context
- self
- end
+ def description
+ description = "assign @#{@variable}"
+ description << " with a kind of #{@expected_class}" if @expected_class
+ description
+ end
- private
-
- def assigned_value?
- if !@controller.instance_variables.include?("@#{@variable}")
- @failure_message =
- "Expected action to assign a value for @#{@variable}"
- false
- else
- @negative_failure_message =
- "Didn't expect action to assign a value for @#{@variable}, " <<
- "but it was assigned to #{assigned_value.inspect}"
- true
- end
- end
+ def in_context(context)
+ @context = context
+ self
+ end
- def kind_of_expected_class?
- return true unless @expected_class
- if assigned_value.kind_of?(@expected_class)
- @negative_failure_message =
- "Didn't expect action to assign a kind of #{@expected_class} " <<
- "for #{@variable}, but got one anyway"
- true
- else
- @failure_message =
- "Expected action to assign a kind of #{@expected_class} " <<
- "for #{@variable}, but got #{@variable.inspect} " <<
- "(#{@variable.class.name})"
- false
- end
+ private
+
+ def assigned_value?
+ if !@controller.instance_variables.include?("@#{@variable}")
+ @failure_message =
+ "Expected action to assign a value for @#{@variable}"
+ false
+ else
+ @negative_failure_message =
+ "Didn't expect action to assign a value for @#{@variable}, " <<
+ "but it was assigned to #{assigned_value.inspect}"
+ true
end
+ end
- def equal_to_expected_value?
- return true unless @check_value
- if @expected_value == assigned_value
- @negative_failure_message =
- "Didn't expect action to assign #{@expected_value.inspect} " <<
- "for #{@variable}, but got it anyway"
- true
- else
- @failure_message =
- "Expected action to assign #{@expected_value.inspect} " <<
- "for #{@variable}, but got #{assigned_value.inspect}"
- false
- end
+ def kind_of_expected_class?
+ return true unless @expected_class
+ if assigned_value.kind_of?(@expected_class)
+ @negative_failure_message =
+ "Didn't expect action to assign a kind of #{@expected_class} " <<
+ "for #{@variable}, but got one anyway"
+ true
+ else
+ @failure_message =
+ "Expected action to assign a kind of #{@expected_class} " <<
+ "for #{@variable}, but got #{@variable.inspect} " <<
+ "(#{@variable.class.name})"
+ false
end
+ end
- def assigned_value
- @controller.instance_variable_get("@#{@variable}")
+ def equal_to_expected_value?
+ return true unless @check_value
+ if @expected_value == assigned_value
+ @negative_failure_message =
+ "Didn't expect action to assign #{@expected_value.inspect} " <<
+ "for #{@variable}, but got it anyway"
+ true
+ else
+ @failure_message =
+ "Expected action to assign #{@expected_value.inspect} " <<
+ "for #{@variable}, but got #{assigned_value.inspect}"
+ false
end
+ end
+ def assigned_value
+ @controller.instance_variable_get("@#{@variable}")
end
end
+
end
end
View
66 lib/shoulda/action_controller/filter_param_matcher.rb
@@ -1,50 +1,48 @@
module Shoulda # :nodoc:
module ActionController # :nodoc:
- module Matchers
-
- # Ensures that filter_parameter_logging is set for the specified key.
- #
- # Example:
- #
- # it { should filter_param(:password) }
- def filter_param(key)
- FilterParamMatcher.new(key)
- end
- class FilterParamMatcher # :nodoc:
+ # Ensures that filter_parameter_logging is set for the specified key.
+ #
+ # Example:
+ #
+ # it { should filter_param(:password) }
+ def filter_param(key)
+ FilterParamMatcher.new(key)
+ end
- def initialize(key)
- @key = key.to_s
- end
+ class FilterParamMatcher # :nodoc:
- def matches?(controller)
- @controller = controller
- filters_key?
- end
+ def initialize(key)
+ @key = key.to_s
+ end
- def failure_message
- "Expected #{@key} to be filtered; filtered keys: #{filtered_keys.join(', ')}"
- end
+ def matches?(controller)
+ @controller = controller
+ filters_key?
+ end
- def negative_failure_message
- "Did not expect #{@key} to be filtered"
- end
+ def failure_message
+ "Expected #{@key} to be filtered; filtered keys: #{filtered_keys.join(', ')}"
+ end
- def description
- "filter #{@key}"
- end
+ def negative_failure_message
+ "Did not expect #{@key} to be filtered"
+ end
- private
+ def description
+ "filter #{@key}"
+ end
- def filters_key?
- filtered_keys.include?(@key)
- end
+ private
- def filtered_keys
- Rails.application.config.filter_parameters.map { |filter| filter.to_s }
- end
+ def filters_key?
+ filtered_keys.include?(@key)
end
+ def filtered_keys
+ Rails.application.config.filter_parameters.map { |filter| filter.to_s }
+ end
end
+
end
end
View
92 lib/shoulda/action_controller/redirect_to_matcher.rb
@@ -1,62 +1,60 @@
module Shoulda # :nodoc:
module ActionController # :nodoc:
- module Matchers
-
- # Ensures a controller redirected to the given url.
- #
- # Example:
- #
- # it { should redirect_to('http://somewhere.com') }
- # it { should redirect_to(users_path) }
- def redirect_to(url_or_description, &block)
- RedirectToMatcher.new(url_or_description, self, &block)
- end
- class RedirectToMatcher # :nodoc:
-
- def initialize(url_or_description, context, &block)
- if block
- @url_block = block
- @location = @url_or_description
- else
- @url = url_or_description
- @location = @url
- end
- @context = context
- end
+ # Ensures a controller redirected to the given url.
+ #
+ # Example:
+ #
+ # it { should redirect_to('http://somewhere.com') }
+ # it { should redirect_to(users_path) }
+ def redirect_to(url_or_description, &block)
+ RedirectToMatcher.new(url_or_description, self, &block)
+ end
- def in_context(context)
- @context = context
- self
- end
+ class RedirectToMatcher # :nodoc:
- def matches?(controller)
- @controller = controller
- redirects_to_url?
+ def initialize(url_or_description, context, &block)
+ if block
+ @url_block = block
+ @location = @url_or_description
+ else
+ @url = url_or_description
+ @location = @url
end
+ @context = context
+ end
- attr_reader :failure_message, :negative_failure_message
+ def in_context(context)
+ @context = context
+ self
+ end
- def description
- "redirect to #{@location}"
- end
+ def matches?(controller)
+ @controller = controller
+ redirects_to_url?
+ end
- private
-
- def redirects_to_url?
- @url = @context.instance_eval(&@url_block) if @url_block
- begin
- @context.send(:assert_redirected_to, @url)
- @negative_failure_message = "Didn't expect to redirect to #{@url}"
- true
- rescue Test::Unit::AssertionFailedError => error
- @failure_message = error.message
- false
- end
- end
+ attr_reader :failure_message, :negative_failure_message
+
+ def description
+ "redirect to #{@location}"
+ end
+ private
+
+ def redirects_to_url?
+ @url = @context.instance_eval(&@url_block) if @url_block
+ begin
+ @context.send(:assert_redirected_to, @url)
+ @negative_failure_message = "Didn't expect to redirect to #{@url}"
+ true
+ rescue Test::Unit::AssertionFailedError => error
+ @failure_message = error.message
+ false
+ end
end
end
+
end
end
View
76 lib/shoulda/action_controller/render_template_matcher.rb
@@ -1,54 +1,52 @@
module Shoulda # :nodoc:
module ActionController # :nodoc:
- module Matchers
-
- # Ensures a controller rendered the given template.
- #
- # Example:
- #
- # it { should render_template(:show) }
- def render_template(template)
- RenderTemplateMatcher.new(template, self)
- end
- class RenderTemplateMatcher # :nodoc:
+ # Ensures a controller rendered the given template.
+ #
+ # Example:
+ #
+ # it { should render_template(:show) }
+ def render_template(template)
+ RenderTemplateMatcher.new(template, self)
+ end
- def initialize(template, context)
- @template = template.to_s
- @context = context
- end
+ class RenderTemplateMatcher # :nodoc:
- def matches?(controller)
- @controller = controller
- renders_template?
- end
+ def initialize(template, context)
+ @template = template.to_s
+ @context = context
+ end
- attr_reader :failure_message, :negative_failure_message
+ def matches?(controller)
+ @controller = controller
+ renders_template?
+ end
- def description
- "render template #{@template}"
- end
+ attr_reader :failure_message, :negative_failure_message
- def in_context(context)
- @context = context
- self
- end
+ def description
+ "render template #{@template}"
+ end
- private
-
- def renders_template?
- begin
- @context.send(:assert_template, @template)
- @negative_failure_message = "Didn't expect to render #{@template}"
- true
- rescue Test::Unit::AssertionFailedError => error
- @failure_message = error.message
- false
- end
- end
+ def in_context(context)
+ @context = context
+ self
+ end
+ private
+
+ def renders_template?
+ begin
+ @context.send(:assert_template, @template)
+ @negative_failure_message = "Didn't expect to render #{@template}"
+ true
+ rescue Test::Unit::AssertionFailedError => error
+ @failure_message = error.message
+ false
+ end
end
end
+
end
end
View
142 lib/shoulda/action_controller/render_with_layout_matcher.rb
@@ -1,99 +1,97 @@
module Shoulda # :nodoc:
module ActionController # :nodoc:
- module Matchers
-
- # Ensures that the controller rendered with the given layout.
- #
- # Example:
- #
- # it { should render_with_layout }
- # it { should render_with_layout(:special) }
- # it { should_not render_with_layout }
- def render_with_layout(expected_layout = nil)
- RenderWithLayoutMatcher.new(expected_layout).in_context(self)
- end
- class RenderWithLayoutMatcher # :nodoc:
+ # Ensures that the controller rendered with the given layout.
+ #
+ # Example:
+ #
+ # it { should render_with_layout }
+ # it { should render_with_layout(:special) }
+ # it { should_not render_with_layout }
+ def render_with_layout(expected_layout = nil)
+ RenderWithLayoutMatcher.new(expected_layout).in_context(self)
+ end
- def initialize(expected_layout)
- @expected_layout = expected_layout.to_s unless expected_layout.nil?
- end
+ class RenderWithLayoutMatcher # :nodoc:
- # Used to provide access to layouts recorded by
- # ActionController::TemplateAssertions in Rails 3
- def in_context(context)
- @context = context
- self
- end
+ def initialize(expected_layout)
+ @expected_layout = expected_layout.to_s unless expected_layout.nil?
+ end
- def matches?(controller)
- @controller = controller
- rendered_with_layout? && rendered_with_expected_layout?
- end
+ # Used to provide access to layouts recorded by
+ # ActionController::TemplateAssertions in Rails 3
+ def in_context(context)
+ @context = context
+ self
+ end
- def failure_message
- "Expected #{expectation}, but #{result}"
- end
+ def matches?(controller)
+ @controller = controller
+ rendered_with_layout? && rendered_with_expected_layout?
+ end
- def negative_failure_message
- "Did not expect #{expectation}, but #{result}"
- end
+ def failure_message
+ "Expected #{expectation}, but #{result}"
+ end
- def description
- description = "render with "
- if @expected_layout.nil?
- description << "a layout"
- else
- description << "the #{@expected_layout.inspect} layout"
- end
- description
+ def negative_failure_message
+ "Did not expect #{expectation}, but #{result}"
+ end
+
+ def description
+ description = "render with "
+ if @expected_layout.nil?
+ description << "a layout"
+ else
+ description << "the #{@expected_layout.inspect} layout"
end
+ description
+ end
- private
+ private
- def rendered_with_layout?
- !rendered_layouts.empty?
- end
+ def rendered_with_layout?
+ !rendered_layouts.empty?
+ end
- def rendered_with_expected_layout?
- return true if @expected_layout.nil?
- rendered_layouts.include?(@expected_layout)
- end
+ def rendered_with_expected_layout?
+ return true if @expected_layout.nil?
+ rendered_layouts.include?(@expected_layout)
+ end
- def rendered_layouts
- if recorded_layouts
- recorded_layouts.keys.compact.map { |layout| layout.sub(%r{^layouts/}, '') }
+ def rendered_layouts
+ if recorded_layouts
+ recorded_layouts.keys.compact.map { |layout| layout.sub(%r{^layouts/}, '') }
+ else
+ layout = @controller.response.layout
+ if layout.nil?
+ []
else
- layout = @controller.response.layout
- if layout.nil?
- []
- else
- [layout.split('/').last]
- end
+ [layout.split('/').last]
end
end
+ end
- def recorded_layouts
- if @context
- @context.instance_variable_get('@layouts')
- end
+ def recorded_layouts
+ if @context
+ @context.instance_variable_get('@layouts')
end
+ end
- def expectation
- "to #{description}"
- end
+ def expectation
+ "to #{description}"
+ end
- def result
- if rendered_with_layout?
- "rendered with " <<
- rendered_layouts.map { |layout| layout.inspect }.join(", ")
- else
- "rendered without a layout"
- end
+ def result
+ if rendered_with_layout?
+ "rendered with " <<
+ rendered_layouts.map { |layout| layout.inspect }.join(", ")
+ else
+ "rendered without a layout"
end
-
end
end
+
end
end
View
106 lib/shoulda/action_controller/respond_with_content_type_matcher.rb
@@ -1,74 +1,72 @@
module Shoulda # :nodoc:
module ActionController # :nodoc:
- module Matchers
- # Ensures a controller responded with expected 'response' content type.
- #
- # You can pass an explicit content type such as 'application/rss+xml'
- # or its symbolic equivalent :rss
- # or a regular expression such as /rss/
- #
- # Example:
- #
- # it { should respond_with_content_type(:xml) }
- # it { should respond_with_content_type(:csv) }
- # it { should respond_with_content_type(:atom) }
- # it { should respond_with_content_type(:yaml) }
- # it { should respond_with_content_type(:text) }
- # it { should respond_with_content_type('application/rss+xml') }
- # it { should respond_with_content_type(/json/) }
- def respond_with_content_type(content_type)
- RespondWithContentTypeMatcher.new(content_type)
- end
+ # Ensures a controller responded with expected 'response' content type.
+ #
+ # You can pass an explicit content type such as 'application/rss+xml'
+ # or its symbolic equivalent :rss
+ # or a regular expression such as /rss/
+ #
+ # Example:
+ #
+ # it { should respond_with_content_type(:xml) }
+ # it { should respond_with_content_type(:csv) }
+ # it { should respond_with_content_type(:atom) }
+ # it { should respond_with_content_type(:yaml) }
+ # it { should respond_with_content_type(:text) }
+ # it { should respond_with_content_type('application/rss+xml') }
+ # it { should respond_with_content_type(/json/) }
+ def respond_with_content_type(content_type)
+ RespondWithContentTypeMatcher.new(content_type)
+ end
- class RespondWithContentTypeMatcher # :nodoc:
+ class RespondWithContentTypeMatcher # :nodoc:
- def initialize(content_type)
- @content_type = if content_type.is_a?(Symbol)
- lookup_by_extension(content_type)
- else
- content_type
- end
- end
-
- def description
- "respond with content type of #{@content_type}"
+ def initialize(content_type)
+ @content_type = if content_type.is_a?(Symbol)
+ lookup_by_extension(content_type)
+ else
+ content_type
end
+ end
- def matches?(controller)
- @controller = controller
- if @content_type.is_a?(Regexp)
- response_content_type =~ @content_type
- else
- response_content_type == @content_type
- end
- end
+ def description
+ "respond with content type of #{@content_type}"
+ end
- def failure_message
- "Expected #{expectation}"
+ def matches?(controller)
+ @controller = controller
+ if @content_type.is_a?(Regexp)
+ response_content_type =~ @content_type
+ else
+ response_content_type == @content_type
end
+ end
- def negative_failure_message
- "Did not expect #{expectation}"
- end
+ def failure_message
+ "Expected #{expectation}"
+ end
- protected
+ def negative_failure_message
+ "Did not expect #{expectation}"
+ end
- def response_content_type
- @controller.response.content_type.to_s
- end
+ protected
- def lookup_by_extension(extension)
- Mime::Type.lookup_by_extension(extension.to_s).to_s
- end
+ def response_content_type
+ @controller.response.content_type.to_s
+ end
- def expectation
- "content type to be #{@content_type}, " <<
- "but was #{response_content_type}"
- end
+ def lookup_by_extension(extension)
+ Mime::Type.lookup_by_extension(extension.to_s).to_s
+ end
+ def expectation
+ "content type to be #{@content_type}, " <<
+ "but was #{response_content_type}"
end
end
+
end
end
View
122 lib/shoulda/action_controller/respond_with_matcher.rb
@@ -1,85 +1,83 @@
module Shoulda # :nodoc:
module ActionController # :nodoc:
- module Matchers
- # Ensures a controller responded with expected 'response' status code.
- #
- # You can pass an explicit status number like 200, 301, 404, 500
- # or its symbolic equivalent :success, :redirect, :missing, :error.
- # See ActionController::StatusCodes for a full list.
- #
- # Example:
- #
- # it { should respond_with(:success) }
- # it { should respond_with(:redirect) }
- # it { should respond_with(:missing) }
- # it { should respond_with(:error) }
- # it { should respond_with(501) }
- def respond_with(status)
- RespondWithMatcher.new(status)
- end
+ # Ensures a controller responded with expected 'response' status code.
+ #
+ # You can pass an explicit status number like 200, 301, 404, 500
+ # or its symbolic equivalent :success, :redirect, :missing, :error.
+ # See ActionController::StatusCodes for a full list.
+ #
+ # Example:
+ #
+ # it { should respond_with(:success) }
+ # it { should respond_with(:redirect) }
+ # it { should respond_with(:missing) }
+ # it { should respond_with(:error) }
+ # it { should respond_with(501) }
+ def respond_with(status)
+ RespondWithMatcher.new(status)
+ end
- class RespondWithMatcher # :nodoc:
+ class RespondWithMatcher # :nodoc:
- def initialize(status)
- @status = symbol_to_status_code(status)
- end
+ def initialize(status)
+ @status = symbol_to_status_code(status)
+ end
- def matches?(controller)
- @controller = controller
- correct_status_code? || correct_status_code_range?
- end
+ def matches?(controller)
+ @controller = controller
+ correct_status_code? || correct_status_code_range?
+ end
- def failure_message
- "Expected #{expectation}"
- end
+ def failure_message
+ "Expected #{expectation}"
+ end
- def negative_failure_message
- "Did not expect #{expectation}"
- end
+ def negative_failure_message
+ "Did not expect #{expectation}"
+ end
- def description
- "respond with #{@status}"
- end
+ def description
+ "respond with #{@status}"
+ end
- protected
+ protected
- def correct_status_code?
- response_code == @status
- end
+ def correct_status_code?
+ response_code == @status
+ end
- def correct_status_code_range?
- @status.is_a?(Range) &&
- @status.include?(response_code)
- end
+ def correct_status_code_range?
+ @status.is_a?(Range) &&
+ @status.include?(response_code)
+ end
- def response_code
- @controller.response.response_code
- end
+ def response_code
+ @controller.response.response_code
+ end
- def symbol_to_status_code(potential_symbol)
- case potential_symbol
- when :success then 200
- when :redirect then 300..399
- when :missing then 404
- when :error then 500..599
- when Symbol
- if defined?(::Rack::Utils::SYMBOL_TO_STATUS_CODE)
- ::Rack::Utils::SYMBOL_TO_STATUS_CODE[potential_symbol]
- else
- ::ActionController::Base::SYMBOL_TO_STATUS_CODE[potential_symbol]
- end
+ def symbol_to_status_code(potential_symbol)
+ case potential_symbol
+ when :success then 200
+ when :redirect then 300..399
+ when :missing then 404
+ when :error then 500..599
+ when Symbol
+ if defined?(::Rack::Utils::SYMBOL_TO_STATUS_CODE)
+ ::Rack::Utils::SYMBOL_TO_STATUS_CODE[potential_symbol]
else
- potential_symbol
+ ::ActionController::Base::SYMBOL_TO_STATUS_CODE[potential_symbol]
end
+ else
+ potential_symbol
end
+ end
- def expectation
- "response to be a #{@status}, but was #{response_code}"
- end
-
+ def expectation
+ "response to be a #{@status}, but was #{response_code}"
end
end
+
end
end
View
140 lib/shoulda/action_controller/route_matcher.rb
@@ -1,93 +1,91 @@
module Shoulda # :nodoc:
module ActionController # :nodoc:
- module Matchers
- # Ensures that requesting +path+ using +method+ routes to +options+.
- #
- # If you don't specify a controller, it will use the controller from the
- # example group.
- #
- # +to_param+ is called on the +options+ given.
- #
- # Examples:
- #
- # it { should route(:get, "/posts").
- # to(:controller => :posts, :action => :index) }
- # it { should route(:get, "/posts/new").to(:action => :new) }
- # it { should route(:post, "/posts").to(:action => :create) }
- # it { should route(:get, "/posts/1").to(:action => :show, :id => 1) }
- # it { should route(:edit, "/posts/1").to(:action => :show, :id => 1) }
- # it { should route(:put, "/posts/1").to(:action => :update, :id => 1) }
- # it { should route(:delete, "/posts/1").
- # to(:action => :destroy, :id => 1) }
- # it { should route(:get, "/users/1/posts/1").
- # to(:action => :show, :id => 1, :user_id => 1) }
- def route(method, path)
- RouteMatcher.new(method, path, self)
- end
+ # Ensures that requesting +path+ using +method+ routes to +options+.
+ #
+ # If you don't specify a controller, it will use the controller from the
+ # example group.
+ #
+ # +to_param+ is called on the +options+ given.
+ #
+ # Examples:
+ #
+ # it { should route(:get, "/posts").
+ # to(:controller => :posts, :action => :index) }
+ # it { should route(:get, "/posts/new").to(:action => :new) }
+ # it { should route(:post, "/posts").to(:action => :create) }
+ # it { should route(:get, "/posts/1").to(:action => :show, :id => 1) }
+ # it { should route(:edit, "/posts/1").to(:action => :show, :id => 1) }
+ # it { should route(:put, "/posts/1").to(:action => :update, :id => 1) }
+ # it { should route(:delete, "/posts/1").
+ # to(:action => :destroy, :id => 1) }
+ # it { should route(:get, "/users/1/posts/1").
+ # to(:action => :show, :id => 1, :user_id => 1) }
+ def route(method, path)
+ RouteMatcher.new(method, path, self)
+ end
- class RouteMatcher # :nodoc:
+ class RouteMatcher # :nodoc:
- def initialize(method, path, context)
- @method = method
- @path = path
- @context = context
- end
+ def initialize(method, path, context)
+ @method = method
+ @path = path
+ @context = context
+ end
- def to(params)
- @params = params
- stringify_params!
- self
- end
+ def to(params)
+ @params = params
+ stringify_params!
+ self
+ end
- def in_context(context)
- @context = context
- self
- end
+ def in_context(context)
+ @context = context
+ self
+ end
- def matches?(controller)
- @controller = controller
- guess_controller!
- route_recognized?
- end
+ def matches?(controller)
+ @controller = controller
+ guess_controller!
+ route_recognized?
+ end
- attr_reader :failure_message, :negative_failure_message
+ attr_reader :failure_message, :negative_failure_message
- def description
- "route #{@method.to_s.upcase} #{@path} to/from #{@params.inspect}"
- end
+ def description
+ "route #{@method.to_s.upcase} #{@path} to/from #{@params.inspect}"
+ end
- private
+ private
- def guess_controller!
- @params[:controller] ||= @controller.controller_path
- end
+ def guess_controller!
+ @params[:controller] ||= @controller.controller_path
+ end
- def stringify_params!
- @params.each do |key, value|
- @params[key] = value.is_a?(Array) ? value.collect {|v| v.to_param } : value.to_param
- end
+ def stringify_params!
+ @params.each do |key, value|
+ @params[key] = value.is_a?(Array) ? value.collect {|v| v.to_param } : value.to_param
end
+ end
- def route_recognized?
- begin
- @context.send(:assert_routing,
- { :method => @method, :path => @path },
- @params)
+ def route_recognized?
+ begin
+ @context.send(:assert_routing,
+ { :method => @method, :path => @path },
+ @params)
- @negative_failure_message = "Didn't expect to #{description}"
- true
- rescue ::ActionController::RoutingError => error
- @failure_message = error.message
- false
- rescue Test::Unit::AssertionFailedError => error
- @failure_message = error.message
- false
- end
+ @negative_failure_message = "Didn't expect to #{description}"
+ true
+ rescue ::ActionController::RoutingError => error
+ @failure_message = error.message
+ false
+ rescue Test::Unit::AssertionFailedError => error
+ @failure_message = error.message
+ false
end
-
end
end
+
end
end
View
144 lib/shoulda/action_controller/set_session_matcher.rb
@@ -1,98 +1,96 @@
module Shoulda # :nodoc:
module ActionController # :nodoc:
- module Matchers
-
- # Ensures that a session key was set to the expected value.
- #
- # Example:
- #
- # it { should set_session(:message) }
- # it { should set_session(:user_id).to(@user.id) }
- # it { should_not set_session(:user_id) }
- def set_session(key)
- SetSessionMatcher.new(key)
- end
- class SetSessionMatcher # :nodoc:
+ # Ensures that a session key was set to the expected value.
+ #
+ # Example:
+ #
+ # it { should set_session(:message) }
+ # it { should set_session(:user_id).to(@user.id) }
+ # it { should_not set_session(:user_id) }
+ def set_session(key)
+ SetSessionMatcher.new(key)
+ end
- def initialize(key)
- @key = key.to_s
- end
+ class SetSessionMatcher # :nodoc:
- def to(value = nil, &block)
- @value = value
- @value_block = block
- self
- end
+ def initialize(key)
+ @key = key.to_s
+ end
- def matches?(controller)
- @controller = controller
- @value = @context.instance_eval(&@value_block) if @value_block
- (assigned_value? && assigned_correct_value?) || cleared_value?
- end
+ def to(value = nil, &block)
+ @value = value
+ @value_block = block
+ self
+ end
- def failure_message
- "Expected #{expectation}, but #{result}"
- end
+ def matches?(controller)
+ @controller = controller
+ @value = @context.instance_eval(&@value_block) if @value_block
+ (assigned_value? && assigned_correct_value?) || cleared_value?
+ end
- def negative_failure_message
- "Didn't expect #{expectation}, but #{result}"
- end
+ def failure_message
+ "Expected #{expectation}, but #{result}"
+ end
- def description
- description = "set session variable #{@key.inspect}"
- description << " to #{@value.inspect}" if defined?(@value)
- description
- end
+ def negative_failure_message
+ "Didn't expect #{expectation}, but #{result}"
+ end
- def in_context(context)
- @context = context
- self
- end
+ def description
+ description = "set session variable #{@key.inspect}"
+ description << " to #{@value.inspect}" if defined?(@value)
+ description
+ end
- private
+ def in_context(context)
+ @context = context
+ self
+ end
- def assigned_value?
- !assigned_value.nil?
- end
+ private
- def cleared_value?
- defined?(@value) && @value.nil? && assigned_value.nil?
- end
+ def assigned_value?
+ !assigned_value.nil?
+ end
- def assigned_correct_value?
- return true if @value.nil?
- assigned_value == @value
- end
+ def cleared_value?
+ defined?(@value) && @value.nil? && assigned_value.nil?
+ end
- def assigned_value
- session[@key]
- end
+ def assigned_correct_value?
+ return true if @value.nil?
+ assigned_value == @value
+ end
- def session
- if @controller.request.respond_to?(:session)
- @controller.request.session.to_hash
- else
- @controller.response.session.data
- end
- end
+ def assigned_value
+ session[@key]
+ end
- def expectation
- expectation = "session variable #{@key} to be set"
- expectation << " to #{@value.inspect}" if @value
- expectation
+ def session
+ if @controller.request.respond_to?(:session)
+ @controller.request.session.to_hash
+ else
+ @controller.response.session.data
end
+ end
- def result
- if session.empty?
- "no session variables were set"
- else
- "the session was #{session.inspect}"
- end
- end
+ def expectation
+ expectation = "session variable #{@key} to be set"
+ expectation << " to #{@value.inspect}" if @value
+ expectation
+ end
+ def result
+ if session.empty?
+ "no session variables were set"
+ else
+ "the session was #{session.inspect}"
+ end
end
end
+
end
end
View
140 lib/shoulda/action_controller/set_the_flash_matcher.rb
@@ -1,94 +1,92 @@
module Shoulda # :nodoc:
module ActionController # :nodoc:
- module Matchers
-
- # Ensures that the flash contains the given value. Can be a String, a
- # Regexp, or nil (indicating that the flash should not be set).
- #
- # Example:
- #
- # it { should set_the_flash }
- # it { should set_the_flash.to("Thank you for placing this order.") }
- # it { should set_the_flash.to(/created/i) }
- # it { should set_the_flash.to(/logged in/i).now }
- # it { should_not set_the_flash }
- def set_the_flash
- SetTheFlashMatcher.new
- end
- class SetTheFlashMatcher # :nodoc:
+ # Ensures that the flash contains the given value. Can be a String, a
+ # Regexp, or nil (indicating that the flash should not be set).
+ #
+ # Example:
+ #
+ # it { should set_the_flash }
+ # it { should set_the_flash.to("Thank you for placing this order.") }
+ # it { should set_the_flash.to(/created/i) }
+ # it { should set_the_flash.to(/logged in/i).now }
+ # it { should_not set_the_flash }
+ def set_the_flash
+ SetTheFlashMatcher.new
+ end
- def to(value)
- @value = value
- self
- end
+ class SetTheFlashMatcher # :nodoc:
- def now
- @now = true
- self
- end
+ def to(value)
+ @value = value
+ self
+ end
- def matches?(controller)
- @controller = controller
- sets_the_flash? && string_value_matches? && regexp_value_matches?
- end
+ def now
+ @now = true
+ self
+ end
- attr_reader :failure_message, :negative_failure_message
+ def matches?(controller)
+ @controller = controller
+ sets_the_flash? && string_value_matches? && regexp_value_matches?
+ end
- def description
- description = "set the flash"
- description << " to #{@value.inspect}" unless @value.nil?
- description
- end
+ attr_reader :failure_message, :negative_failure_message
- def failure_message
- "Expected #{expectation}"
- end
+ def description
+ description = "set the flash"
+ description << " to #{@value.inspect}" unless @value.nil?
+ description
+ end
- def negative_failure_message
- "Did not expect #{expectation}"
- end
+ def failure_message
+ "Expected #{expectation}"
+ end
- private
+ def negative_failure_message
+ "Did not expect #{expectation}"
+ end
- def sets_the_flash?
- !flash.blank?
- end
+ private
- def string_value_matches?
- return true unless String === @value
- flash.values.any? {|value| value == @value }
- end
+ def sets_the_flash?
+ !flash.blank?
+ end
- def regexp_value_matches?
- return true unless Regexp === @value
- flash.values.any? {|value| value =~ @value }
- end
+ def string_value_matches?
+ return true unless String === @value
+ flash.values.any? {|value| value == @value }
+ end
- def flash
- return @flash if @flash
- @flash = @controller.flash.dup
- @flash.sweep unless @now
- @flash
- end
+ def regexp_value_matches?
+ return true unless Regexp === @value
+ flash.values.any? {|value| value =~ @value }
+ end
- def expectation
- expectation = "the flash#{".now" if @now} to be set"
- expectation << " to #{@value.inspect}" unless @value.nil?
- expectation << ", but #{flash_description}"
- expectation
- end
+ def flash
+ return @flash if @flash
+ @flash = @controller.flash.dup
+ @flash.sweep unless @now
+ @flash
+ end
- def flash_description
- if flash.blank?
- "no flash was set"
- else
- "was #{flash.inspect}"
- end
- end
+ def expectation
+ expectation = "the flash#{".now" if @now} to be set"
+ expectation << " to #{@value.inspect}" unless @value.nil?
+ expectation << ", but #{flash_description}"
+ expectation
+ end
+ def flash_description
+ if flash.blank?
+ "no flash was set"
+ else
+ "was #{flash.inspect}"
+ end
end
end
+
end
end
View
34 lib/shoulda/action_mailer.rb
@@ -1,22 +1,20 @@
require 'shoulda/action_mailer/have_sent_email'
-module Shoulda # :nodoc:
- module ActionMailer # :nodoc:
- # = Matchers for your mailers
- #
- # This matcher will test that email is sent properly
- #
- # describe User do
- # it { should have_sent_email.with_subject(/is spam$/) }
- # it { should have_sent_email.from('do-not-reply@example.com') }
- # it { should have_sent_email.with_body(/is spam\./) }
- # it { should have_sent_email.to('myself@me.com') }
- # it { should have_sent_email.with_subject(/spam/).
- # from('do-not-reply@example.com').
- # with_body(/spam/).
- # to('myself@me.com') }
- # end
- module Matchers
- end
+module Shoulda
+ # = Matchers for your mailers
+ #
+ # This matcher will test that email is sent properly
+ #
+ # describe User do
+ # it { should have_sent_email.with_subject(/is spam$/) }
+ # it { should have_sent_email.from('do-not-reply@example.com') }
+ # it { should have_sent_email.with_body(/is spam\./) }
+ # it { should have_sent_email.to('myself@me.com') }
+ # it { should have_sent_email.with_subject(/spam/).
+ # from('do-not-reply@example.com').
+ # with_body(/spam/).
+ # to('myself@me.com') }
+ # end
+ module ActionMailer
end
end
View
164 lib/shoulda/action_mailer/have_sent_email.rb
@@ -1,107 +1,105 @@
module Shoulda # :nodoc:
module ActionMailer # :nodoc:
- module Matchers
-
- # The right email is sent.
- #
- # it { should have_sent_email.with_subject(/is spam$/) }
- # it { should have_sent_email.from('do-not-reply@example.com') }
- # it { should have_sent_email.with_body(/is spam\./) }
- # it { should have_sent_email.to('myself@me.com') }
- # it { should have_sent_email.with_subject(/spam/).
- # from('do-not-reply@example.com').
- # with_body(/spam/).
- # to('myself@me.com') }
- def have_sent_email
- HaveSentEmailMatcher.new
- end
- class HaveSentEmailMatcher # :nodoc:
+ # The right email is sent.
+ #
+ # it { should have_sent_email.with_subject(/is spam$/) }
+ # it { should have_sent_email.from('do-not-reply@example.com') }
+ # it { should have_sent_email.with_body(/is spam\./) }
+ # it { should have_sent_email.to('myself@me.com') }
+ # it { should have_sent_email.with_subject(/spam/).
+ # from('do-not-reply@example.com').
+ # with_body(/spam/).
+ # to('myself@me.com') }
+ def have_sent_email
+ HaveSentEmailMatcher.new
+ end
- def initialize
- end
+ class HaveSentEmailMatcher # :nodoc:
- def with_subject(email_subject)
- @email_subject = email_subject
- self
- end
+ def initialize
+ end
- def from(sender)
- @sender = sender
- self
- end
+ def with_subject(email_subject)
+ @email_subject = email_subject
+ self
+ end
- def with_body(body)
- @body = body
- self
- end
+ def from(sender)
+ @sender = sender
+ self
+ end
- def to(recipient)
- @recipient = recipient
- self
- end
+ def with_body(body)
+ @body = body
+ self
+ end
- def matches?(subject)
- ::ActionMailer::Base.deliveries.each do |mail|
- @subject_failed = !regexp_or_string_match(mail.subject, @email_subject) if @email_subject
- @body_failed = !regexp_or_string_match(mail.body, @body) if @body
- @sender_failed = !regexp_or_string_match_in_array(mail.from, @sender) if @sender
- @recipient_failed = !regexp_or_string_match_in_array(mail.to, @recipient) if @recipient
- return true unless anything_failed?
- end
+ def to(recipient)
+ @recipient = recipient
+ self
+ end
- false
+ def matches?(subject)
+ ::ActionMailer::Base.deliveries.each do |mail|
+ @subject_failed = !regexp_or_string_match(mail.subject, @email_subject) if @email_subject
+ @body_failed = !regexp_or_string_match(mail.body, @body) if @body
+ @sender_failed = !regexp_or_string_match_in_array(mail.from, @sender) if @sender
+ @recipient_failed = !regexp_or_string_match_in_array(mail.to, @recipient) if @recipient
+ return true unless anything_failed?
end
- def failure_message
- "Expected #{expectation}"
- end
+ false
+ end
- def negative_failure_message
- "Did not expect #{expectation}"
- end
+ def failure_message
+ "Expected #{expectation}"
+ end
- def description
- "send an email"
- end
+ def negative_failure_message
+ "Did not expect #{expectation}"
+ end
- private
+ def description
+ "send an email"
+ end
- def expectation
- expectation = "sent email"
- expectation << " with subject #{@email_subject.inspect}" if @subject_failed
- expectation << " with body #{@body.inspect}" if @body_failed
- expectation << " from #{@sender.inspect}" if @sender_failed
- expectation << " to #{@recipient.inspect}" if @recipient_failed
- expectation << "\nDeliveries:\n#{inspect_deliveries}"
- end
+ private
- def inspect_deliveries
- ::ActionMailer::Base.deliveries.map do |delivery|
- "#{delivery.subject.inspect} to #{delivery.to.inspect}"
- end.join("\n")
- end
+ def expectation
+ expectation = "sent email"
+ expectation << " with subject #{@email_subject.inspect}" if @subject_failed
+ expectation << " with body #{@body.inspect}" if @body_failed
+ expectation << " from #{@sender.inspect}" if @sender_failed
+ expectation << " to #{@recipient.inspect}" if @recipient_failed
+ expectation << "\nDeliveries:\n#{inspect_deliveries}"
+ end
- def anything_failed?
- @subject_failed || @body_failed || @sender_failed || @recipient_failed
- end
+ def inspect_deliveries
+ ::ActionMailer::Base.deliveries.map do |delivery|
+ "#{delivery.subject.inspect} to #{delivery.to.inspect}"
+ end.join("\n")
+ end
- def regexp_or_string_match(a_string, a_regexp_or_string)
- case a_regexp_or_string
- when Regexp
- a_string =~ a_regexp_or_string
- when String
- a_string == a_regexp_or_string
- end
+ def anything_failed?
+ @subject_failed || @body_failed || @sender_failed || @recipient_failed
+ end
+
+ def regexp_or_string_match(a_string, a_regexp_or_string)
+ case a_regexp_or_string
+ when Regexp
+ a_string =~ a_regexp_or_string
+ when String
+ a_string == a_regexp_or_string
end
+ end
- def regexp_or_string_match_in_array(an_array, a_regexp_or_string)
- case a_regexp_or_string
- when Regexp
- an_array.any? { |string| string =~ a_regexp_or_string }
- when String
- an_array.include?(a_regexp_or_string)
- end
+ def regexp_or_string_match_in_array(an_array, a_regexp_or_string)
+ case a_regexp_or_string
+ when Regexp
+ an_array.any? { |string| string =~ a_regexp_or_string }
+ when String
+ an_array.include?(a_regexp_or_string)
end
end
end
View
44 lib/shoulda/active_record.rb
@@ -15,28 +15,26 @@
require 'shoulda/active_record/allow_mass_assignment_of_matcher'
-module Shoulda # :nodoc:
- module ActiveRecord # :nodoc:
- # = Matchers for your active record models
- #
- # These matchers will test most of the validations and associations for your
- # ActiveRecord models.
- #
- # describe User do
- # it { should validate_presence_of(:name) }
- # it { should validate_presence_of(:phone_number) }
- # %w(abcd 1234).each do |value|
- # it { should_not allow_value(value).for(:phone_number) }
- # end
- # it { should allow_value("(123) 456-7890").for(:phone_number) }
- # it { should_not allow_mass_assignment_of(:password) }
- # it { should have_one(:profile) }
- # it { should have_many(:dogs) }
- # it { should have_many(:messes).through(:dogs) }
- # it { should belong_to(:lover) }
- # end
- #
- module Matchers
- end
+module Shoulda
+ # = Matchers for your active record models
+ #
+ # These matchers will test most of the validations and associations for your
+ # ActiveRecord models.
+ #
+ # describe User do
+ # it { should validate_presence_of(:name) }
+ # it { should validate_presence_of(:phone_number) }
+ # %w(abcd 1234).each do |value|
+ # it { should_not allow_value(value).for(:phone_number) }
+ # end
+ # it { should allow_value("(123) 456-7890").for(:phone_number) }
+ # it { should_not allow_mass_assignment_of(:password) }
+ # it { should have_one(:profile) }
+ # it { should have_many(:dogs) }
+ # it { should have_many(:messes).through(:dogs) }
+ # it { should belong_to(:lover) }
+ # end
+ #
+ module ActiveRecord
end
end
View
114 lib/shoulda/active_record/allow_mass_assignment_of_matcher.rb
@@ -1,83 +1,81 @@
module Shoulda # :nodoc:
module ActiveRecord # :nodoc:
- module Matchers
- # Ensures that the attribute can be set on mass update.
- #
- # it { should_not allow_mass_assignment_of(:password) }
- # it { should allow_mass_assignment_of(:first_name) }
- #
- def allow_mass_assignment_of(value)
- AllowMassAssignmentOfMatcher.new(value)
- end
+ # Ensures that the attribute can be set on mass update.
+ #
+ # it { should_not allow_mass_assignment_of(:password) }
+ # it { should allow_mass_assignment_of(:first_name) }
+ #
+ def allow_mass_assignment_of(value)
+ AllowMassAssignmentOfMatcher.new(value)
+ end
- class AllowMassAssignmentOfMatcher # :nodoc:
+ class AllowMassAssignmentOfMatcher # :nodoc:
- def initialize(attribute)
- @attribute = attribute.to_s
- end
+ def initialize(attribute)
+ @attribute = attribute.to_s
+ end
- def matches?(subject)
- @subject = subject
- if attr_mass_assignable?
- if whitelisting?
- @negative_failure_message = "#{@attribute} was made accessible"
- else
- if protected_attributes.empty?
- @negative_failure_message = "no attributes were protected"
- else
- @negative_failure_message = "#{class_name} is protecting " <<
- "#{protected_attributes.to_a.to_sentence}, " <<
- "but not #{@attribute}."
- end
- end
- true
+ def matches?(subject)
+ @subject = subject
+ if attr_mass_assignable?
+ if whitelisting?
+ @negative_failure_message = "#{@attribute} was made accessible"
else
- if whitelisting?
- @failure_message =
- "Expected #{@attribute} to be accessible"
+ if protected_attributes.empty?
+ @negative_failure_message = "no attributes were protected"
else
- @failure_message =
- "Did not expect #{@attribute} to be protected"
+ @negative_failure_message = "#{class_name} is protecting " <<
+ "#{protected_attributes.to_a.to_sentence}, " <<
+ "but not #{@attribute}."
end
- false
end
+ true
+ else
+ if whitelisting?
+ @failure_message =
+ "Expected #{@attribute} to be accessible"
+ else
+ @failure_message =
+ "Did not expect #{@attribute} to be protected"
+ end
+ false
end
+ end
- attr_reader :failure_message, :negative_failure_message
-
- def description
- "allow mass assignment of #{@attribute}"
- end
+ attr_reader :failure_message, :negative_failure_message
- private
+ def description
+ "allow mass assignment of #{@attribute}"
+ end
- def protected_attributes
- @protected_attributes ||= (@subject.class.protected_attributes || [])
- end
+ private
- def accessible_attributes
- @accessible_attributes ||= (@subject.class.accessible_attributes || [])
- end
+ def protected_attributes
+ @protected_attributes ||= (@subject.class.protected_attributes || [])
+ end
- def whitelisting?
- !accessible_attributes.empty?
- end
+ def accessible_attributes
+ @accessible_attributes ||= (@subject.class.accessible_attributes || [])
+ end
- def attr_mass_assignable?
- if whitelisting?
- accessible_attributes.include?(@attribute)
- else
- !protected_attributes.include?(@attribute)
- end
- end
+ def whitelisting?
+ !accessible_attributes.empty?
+ end
- def class_name
- @subject.class.name
+ def attr_mass_assignable?
+ if whitelisting?
+ accessible_attributes.include?(@attribute)
+ else
+ !protected_attributes.include?(@attribute)
end
+ end
+ def class_name
+ @subject.class.name
end
end
+
end
end
View
166 lib/shoulda/active_record/allow_value_matcher.rb
@@ -1,110 +1,108 @@
module Shoulda # :nodoc:
module ActiveRecord # :nodoc:
- module Matchers
-
- # Ensures that the attribute can be set to the given value.
- #
- # Options:
- # * <tt>with_message</tt> - value the test expects to find in
- # <tt>errors.on(:attribute)</tt>. Regexp or string. If omitted,
- # the test looks for any errors in <tt>errors.on(:attribute)</tt>.
- #
- # Example:
- # it { should_not allow_value('bad').for(:isbn) }
- # it { should allow_value("isbn 1 2345 6789 0").for(:isbn) }
- #
- def allow_value(value)
- AllowValueMatcher.new(value)
- end
- class AllowValueMatcher # :nodoc:
- include Helpers
+ # Ensures that the attribute can be set to the given value.
+ #
+ # Options:
+ # * <tt>with_message</tt> - value the test expects to find in
+ # <tt>errors.on(:attribute)</tt>. Regexp or string. If omitted,
+ # the test looks for any errors in <tt>errors.on(:attribute)</tt>.
+ #
+ # Example:
+ # it { should_not allow_value('bad').for(:isbn) }
+ # it { should allow_value("isbn 1 2345 6789 0").for(:isbn) }
+ #
+ def allow_value(value)
+ AllowValueMatcher.new(value)
+ end
- def initialize(value)
- @value = value
- end
+ class AllowValueMatcher # :nodoc:
+ include Helpers
- def for(attribute)
- @attribute = attribute
- self
- end
+ def initialize(value)
+ @value = value
+ end
- def with_message(message)
- @expected_message = message if message
- self
- end
+ def for(attribute)
+ @attribute = attribute
+ self
+ end
- def matches?(instance)
- @instance = instance
- if Symbol === @expected_message
- @expected_message = default_error_message(@expected_message)
- end
- @instance.send("#{@attribute}=", @value)
- !errors_match?
- end
+ def with_message(message)
+ @expected_message = message if message
+ self
+ end
- def failure_message
- "Did not expect #{expectation}, got error: #{@matched_error}"
+ def matches?(instance)
+ @instance = instance
+ if Symbol === @expected_message
+ @expected_message = default_error_message(@expected_message)
end
+ @instance.send("#{@attribute}=", @value)
+ !errors_match?
+ end
- def negative_failure_message
- "Expected #{expectation}, got #{error_description}"
- end
+ def failure_message
+ "Did not expect #{expectation}, got error: #{@matched_error}"
+ end
- def description
- "allow #{@attribute} to be set to #{@value.inspect}"
- end
+ def negative_failure_message
+ "Expected #{expectation}, got #{error_description}"
+ end
- private
+ def description
+ "allow #{@attribute} to be set to #{@value.inspect}"
+ end
- def errors_match?
- @instance.valid?
- @errors = errors_for_attribute(@instance, @attribute)
- @errors = [@errors] unless @errors.is_a?(Array)
- @expected_message ? (errors_match_regexp? || errors_match_string?) : (@errors.compact.any?)
- end
+ private
- def errors_for_attribute(instance, attribute)
- if instance.errors.respond_to?(:[])
- instance.errors[attribute]
- else
- instance.errors.on(attribute)
- end
- end
+ def errors_match?
+ @instance.valid?
+ @errors = errors_for_attribute(@instance, @attribute)
+ @errors = [@errors] unless @errors.is_a?(Array)
+ @expected_message ? (errors_match_regexp? || errors_match_string?) : (@errors.compact.any?)
+ end
- def errors_match_regexp?
- if Regexp === @expected_message
- @matched_error = @errors.detect { |e| e =~ @expected_message }
- !@matched_error.nil?
- else
- false
- end
+ def errors_for_attribute(instance, attribute)
+ if instance.errors.respond_to?(:[])
+ instance.errors[attribute]
+ else
+ instance.errors.on(attribute)
end
+ end
- def errors_match_string?
- if @errors.include?(@expected_message)
- @matched_error = @expected_message
- true
- else
- false
- end
+ def errors_match_regexp?
+ if Regexp === @expected_message
+ @matched_error = @errors.detect { |e| e =~ @expected_message }
+ !@matched_error.nil?
+ else
+ false
end
+ end
- def expectation
- "errors " <<
- (@expected_message ? "to include #{@expected_message.inspect} " : "") <<
- "when #{@attribute} is set to #{@value.inspect}"
+ def errors_match_string?
+ if @errors.include?(@expected_message)
+ @matched_error = @expected_message
+ true
+ else
+ false
end
+ end
- def error_description
- if @instance.errors.empty?
- "no errors"
- else
- "errors: #{pretty_error_messages(@instance)}"
- end
- end
+ def expectation
+ "errors " <<
+ (@expected_message ? "to include #{@expected_message.inspect} " : "") <<
+ "when #{@attribute} is set to #{@value.inspect}"
end
+ def error_description
+ if @instance.errors.empty?
+ "no errors"
+ else
+ "errors: #{pretty_error_messages(@instance)}"
+ end
+ end
end
+
end
end
View
368 lib/shoulda/active_record/association_matcher.rb
@@ -1,226 +1,224 @@
module Shoulda # :nodoc:
module ActiveRecord # :nodoc:
- module Matchers
-
- # Ensure that the belongs_to relationship exists.
- #
- # it { should belong_to(:parent) }
- #
- def belong_to(name)
- AssociationMatcher.new(:belongs_to, name)
- end
-
- # Ensures that the has_many relationship exists. Will also test that the
- # associated table has the required columns. Works with polymorphic
- # associations.
- #
- # Options:
- # * <tt>through</tt> - association name for <tt>has_many :through</tt>
- # * <tt>dependent</tt> - tests that the association makes use of the
- # dependent option.
- #
- # Example:
- # it { should have_many(:friends) }
- # it { should have_many(:enemies).through(:friends) }
- # it { should have_many(:enemies).dependent(:destroy) }
- #
- def have_many(name)
- AssociationMatcher.new(:has_many, name)
- end
-
- # Ensure that the has_one relationship exists. Will also test that the
- # associated table has the required columns. Works with polymorphic
- # associations.
- #
- # Options:
- # * <tt>:dependent</tt> - tests that the association makes use of the
- # dependent option.
- #
- # Example:
- # it { should have_one(:god) } # unless hindu
- #
- def have_one(name)
- AssociationMatcher.new(:has_one, name)
- end
-
- # Ensures that the has_and_belongs_to_many relationship exists, and that
- # the join table is in place.
- #
- # it { should have_and_belong_to_many(:posts) }
- #
- def have_and_belong_to_many(name)
- AssociationMatcher.new(:has_and_belongs_to_many, name)
- end
-
- class AssociationMatcher # :nodoc:
- def initialize(macro, name)
- @macro = macro
- @name = name
- end
- def through(through)
- @through = through
- self
- end
+ # Ensure that the belongs_to relationship exists.
+ #
+ # it { should belong_to(:parent) }
+ #
+ def belong_to(name)
+ AssociationMatcher.new(:belongs_to, name)
+ end
- def dependent(dependent)
- @dependent = dependent
- self
- end
+ # Ensures that the has_many relationship exists. Will also test that the
+ # associated table has the required columns. Works with polymorphic
+ # associations.
+ #
+ # Options:
+ # * <tt>through</tt> - association name for <tt>has_many :through</tt>
+ # * <tt>dependent</tt> - tests that the association makes use of the
+ # dependent option.
+ #
+ # Example:
+ # it { should have_many(:friends) }
+ # it { should have_many(:enemies).through(:friends) }
+ # it { should have_many(:enemies).dependent(:destroy) }
+ #
+ def have_many(name)
+ AssociationMatcher.new(:has_many, name)
+ end
- def matches?(subject)
- @subject = subject
- association_exists? &&
- macro_correct? &&
- foreign_key_exists? &&
- through_association_valid? &&
- dependent_correct? &&
- join_table_exists?
- end
+ # Ensure that the has_one relationship exists. Will also test that the
+ # associated table has the required columns. Works with polymorphic
+ # associations.
+ #
+ # Options:
+ # * <tt>:dependent</tt> - tests that the association makes use of the
+ # dependent option.
+ #
+ # Example:
+ # it { should have_one(:god) } # unless hindu
+ #
+ def have_one(name)
+ AssociationMatcher.new(:has_one, name)
+ end
- def failure_message
- "Expected #{expectation} (#{@missing})"
- end
+ # Ensures that the has_and_belongs_to_many relationship exists, and that
+ # the join table is in place.
+ #