diff --git a/README.rdoc b/README.rdoc index dc1d4f1e..c82965f5 100644 --- a/README.rdoc +++ b/README.rdoc @@ -10,12 +10,22 @@ about using shoulda with Test::Unit. === ActiveRecord Matchers -Matchers to test associations and validations: +Matchers to test associations: describe Post do it { should belong_to(:user) } it { should have_many(:tags).through(:taggings) } + end + describe User do + it { should have_many(:posts) } + end + +=== ActiveModel Matchers + +Matchers to test validations: + + describe Post do it { should validate_uniqueness_of(:title) } it { should validate_presence_of(:body).with_message(/wtf/) } it { should validate_presence_of(:title) } @@ -23,8 +33,6 @@ Matchers to test associations and validations: end describe User do - it { should have_many(:posts) } - it { should_not allow_value("blah").for(:email) } it { should allow_value("a@b.com").for(:email) } it { should ensure_inclusion_of(:age).in_range(1..100) } diff --git a/lib/shoulda/matchers/active_model.rb b/lib/shoulda/matchers/active_model.rb new file mode 100644 index 00000000..8fd6e11d --- /dev/null +++ b/lib/shoulda/matchers/active_model.rb @@ -0,0 +1,33 @@ +require 'shoulda/matchers/active_model/helpers' +require 'shoulda/matchers/active_model/validation_matcher' +require 'shoulda/matchers/active_model/allow_value_matcher' +require 'shoulda/matchers/active_model/ensure_length_of_matcher' +require 'shoulda/matchers/active_model/ensure_inclusion_of_matcher' +require 'shoulda/matchers/active_model/validate_presence_of_matcher' +require 'shoulda/matchers/active_model/validate_format_of_matcher' +require 'shoulda/matchers/active_model/validate_uniqueness_of_matcher' +require 'shoulda/matchers/active_model/validate_acceptance_of_matcher' +require 'shoulda/matchers/active_model/validate_numericality_of_matcher' +require 'shoulda/matchers/active_model/allow_mass_assignment_of_matcher' + + +module Shoulda + module Matchers + # = Matchers for your active record models + # + # These matchers will test most of the validations of ActiveModel::Validations. + # + # 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) } + # end + # + module ActiveModel + end + end +end diff --git a/lib/shoulda/matchers/active_record/allow_mass_assignment_of_matcher.rb b/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb similarity index 98% rename from lib/shoulda/matchers/active_record/allow_mass_assignment_of_matcher.rb rename to lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb index 10a63714..84fa1f44 100644 --- a/lib/shoulda/matchers/active_record/allow_mass_assignment_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb @@ -1,6 +1,6 @@ module Shoulda # :nodoc: module Matchers - module ActiveRecord # :nodoc: + module ActiveModel # :nodoc: # Ensures that the attribute can be set on mass update. # diff --git a/lib/shoulda/matchers/active_record/allow_value_matcher.rb b/lib/shoulda/matchers/active_model/allow_value_matcher.rb similarity index 98% rename from lib/shoulda/matchers/active_record/allow_value_matcher.rb rename to lib/shoulda/matchers/active_model/allow_value_matcher.rb index aacdb86a..f63daa0d 100644 --- a/lib/shoulda/matchers/active_record/allow_value_matcher.rb +++ b/lib/shoulda/matchers/active_model/allow_value_matcher.rb @@ -1,6 +1,6 @@ module Shoulda # :nodoc: module Matchers - module ActiveRecord # :nodoc: + module ActiveModel # :nodoc: # Ensures that the attribute can be set to the given value. # diff --git a/lib/shoulda/matchers/active_record/ensure_inclusion_of_matcher.rb b/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb similarity index 98% rename from lib/shoulda/matchers/active_record/ensure_inclusion_of_matcher.rb rename to lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb index 0e75deca..e8be3deb 100644 --- a/lib/shoulda/matchers/active_record/ensure_inclusion_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb @@ -1,6 +1,6 @@ module Shoulda # :nodoc: module Matchers - module ActiveRecord # :nodoc: + module ActiveModel # :nodoc: # Ensure that the attribute's value is in the range specified # diff --git a/lib/shoulda/matchers/active_record/ensure_length_of_matcher.rb b/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb similarity index 99% rename from lib/shoulda/matchers/active_record/ensure_length_of_matcher.rb rename to lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb index b13d04b4..3afae032 100644 --- a/lib/shoulda/matchers/active_record/ensure_length_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb @@ -1,6 +1,6 @@ module Shoulda # :nodoc: module Matchers - module ActiveRecord # :nodoc: + module ActiveModel # :nodoc: # Ensures that the length of the attribute is validated. # diff --git a/lib/shoulda/matchers/active_record/helpers.rb b/lib/shoulda/matchers/active_model/helpers.rb similarity index 96% rename from lib/shoulda/matchers/active_record/helpers.rb rename to lib/shoulda/matchers/active_model/helpers.rb index 1ff7a582..d735d418 100644 --- a/lib/shoulda/matchers/active_record/helpers.rb +++ b/lib/shoulda/matchers/active_model/helpers.rb @@ -1,6 +1,6 @@ module Shoulda # :nodoc: module Matchers - module ActiveRecord # :nodoc: + module ActiveModel # :nodoc: module Helpers def pretty_error_messages(obj) # :nodoc: obj.errors.map do |a, m| diff --git a/lib/shoulda/matchers/active_record/validate_acceptance_of_matcher.rb b/lib/shoulda/matchers/active_model/validate_acceptance_of_matcher.rb similarity index 96% rename from lib/shoulda/matchers/active_record/validate_acceptance_of_matcher.rb rename to lib/shoulda/matchers/active_model/validate_acceptance_of_matcher.rb index 2f25a0fe..aff1d849 100644 --- a/lib/shoulda/matchers/active_record/validate_acceptance_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/validate_acceptance_of_matcher.rb @@ -1,6 +1,6 @@ module Shoulda # :nodoc: module Matchers - module ActiveRecord # :nodoc: + module ActiveModel # :nodoc: # Ensures that the model cannot be saved the given attribute is not # accepted. diff --git a/lib/shoulda/matchers/active_record/validate_format_of_matcher.rb b/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb similarity index 98% rename from lib/shoulda/matchers/active_record/validate_format_of_matcher.rb rename to lib/shoulda/matchers/active_model/validate_format_of_matcher.rb index fac78169..48c28048 100644 --- a/lib/shoulda/matchers/active_record/validate_format_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb @@ -1,6 +1,6 @@ module Shoulda # :nodoc: module Matchers - module ActiveRecord # :nodoc: + module ActiveModel # :nodoc: # Ensures that the model is not valid if the given attribute is not # formatted correctly. diff --git a/lib/shoulda/matchers/active_record/validate_numericality_of_matcher.rb b/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb similarity index 96% rename from lib/shoulda/matchers/active_record/validate_numericality_of_matcher.rb rename to lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb index fac6c3a7..02561030 100644 --- a/lib/shoulda/matchers/active_record/validate_numericality_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb @@ -1,6 +1,6 @@ module Shoulda # :nodoc: module Matchers - module ActiveRecord # :nodoc: + module ActiveModel # :nodoc: # Ensure that the attribute is numeric # diff --git a/lib/shoulda/matchers/active_record/validate_presence_of_matcher.rb b/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb similarity index 97% rename from lib/shoulda/matchers/active_record/validate_presence_of_matcher.rb rename to lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb index 088a1cd8..eaef3fcd 100644 --- a/lib/shoulda/matchers/active_record/validate_presence_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb @@ -1,6 +1,6 @@ module Shoulda # :nodoc: module Matchers - module ActiveRecord # :nodoc: + module ActiveModel # :nodoc: # Ensures that the model is not valid if the given attribute is not # present. diff --git a/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb b/lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb similarity index 99% rename from lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb rename to lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb index b54c2591..3ec59683 100644 --- a/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb @@ -1,6 +1,6 @@ module Shoulda # :nodoc: module Matchers - module ActiveRecord # :nodoc: + module ActiveModel # :nodoc: # Ensures that the model is invalid if the given attribute is not unique. # diff --git a/lib/shoulda/matchers/active_record/validation_matcher.rb b/lib/shoulda/matchers/active_model/validation_matcher.rb similarity index 97% rename from lib/shoulda/matchers/active_record/validation_matcher.rb rename to lib/shoulda/matchers/active_model/validation_matcher.rb index 086dffa6..8863dced 100644 --- a/lib/shoulda/matchers/active_record/validation_matcher.rb +++ b/lib/shoulda/matchers/active_model/validation_matcher.rb @@ -1,6 +1,6 @@ module Shoulda # :nodoc: module Matchers - module ActiveRecord # :nodoc: + module ActiveModel # :nodoc: class ValidationMatcher # :nodoc: diff --git a/lib/shoulda/matchers/active_record.rb b/lib/shoulda/matchers/active_record.rb index 688df881..9c38cc89 100644 --- a/lib/shoulda/matchers/active_record.rb +++ b/lib/shoulda/matchers/active_record.rb @@ -1,35 +1,17 @@ -require 'shoulda/matchers/active_record/helpers' -require 'shoulda/matchers/active_record/validation_matcher' -require 'shoulda/matchers/active_record/allow_value_matcher' -require 'shoulda/matchers/active_record/ensure_length_of_matcher' -require 'shoulda/matchers/active_record/ensure_inclusion_of_matcher' -require 'shoulda/matchers/active_record/validate_presence_of_matcher' -require 'shoulda/matchers/active_record/validate_format_of_matcher' -require 'shoulda/matchers/active_record/validate_uniqueness_of_matcher' -require 'shoulda/matchers/active_record/validate_acceptance_of_matcher' -require 'shoulda/matchers/active_record/validate_numericality_of_matcher' require 'shoulda/matchers/active_record/association_matcher' require 'shoulda/matchers/active_record/have_db_column_matcher' require 'shoulda/matchers/active_record/have_db_index_matcher' require 'shoulda/matchers/active_record/have_readonly_attribute_matcher' -require 'shoulda/matchers/active_record/allow_mass_assignment_of_matcher' module Shoulda module Matchers # = Matchers for your active record models # - # These matchers will test most of the validations and associations for your + # These matchers will test the 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) } diff --git a/lib/shoulda/matchers/integrations/rspec.rb b/lib/shoulda/matchers/integrations/rspec.rb index 2e1b9286..b2cc9807 100644 --- a/lib/shoulda/matchers/integrations/rspec.rb +++ b/lib/shoulda/matchers/integrations/rspec.rb @@ -7,6 +7,13 @@ if defined?(::ActiveRecord) end end +if defined?(::ActiveModel) + require 'shoulda/matchers/active_model' + module RSpec::Matchers + include Shoulda::Matchers::ActiveModel + end +end + if defined?(::ActionController) require 'shoulda/matchers/action_controller' module RSpec diff --git a/lib/shoulda/matchers/integrations/test_unit.rb b/lib/shoulda/matchers/integrations/test_unit.rb index 26fb24dc..f23b88b2 100644 --- a/lib/shoulda/matchers/integrations/test_unit.rb +++ b/lib/shoulda/matchers/integrations/test_unit.rb @@ -26,7 +26,7 @@ if defined?(ActionMailer) end end -if defined?(ActiveRecord) +if defined?(ActionRecord) require 'shoulda/matchers/active_record' module Test @@ -39,3 +39,15 @@ if defined?(ActiveRecord) end end +if defined?(ActiveModel) + require 'shoulda/matchers/active_model' + + module Test + module Unit + class TestCase + include Shoulda::Matchers::ActiveModel + extend Shoulda::Matchers::ActiveModel + end + end + end +end diff --git a/spec/shoulda/active_record/allow_mass_assignment_of_matcher_spec.rb b/spec/shoulda/active_model/allow_mass_assignment_of_matcher_spec.rb similarity index 96% rename from spec/shoulda/active_record/allow_mass_assignment_of_matcher_spec.rb rename to spec/shoulda/active_model/allow_mass_assignment_of_matcher_spec.rb index 30e92898..b5774518 100644 --- a/spec/shoulda/active_record/allow_mass_assignment_of_matcher_spec.rb +++ b/spec/shoulda/active_model/allow_mass_assignment_of_matcher_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Shoulda::Matchers::ActiveRecord::AllowMassAssignmentOfMatcher do +describe Shoulda::Matchers::ActiveModel::AllowMassAssignmentOfMatcher do context "an attribute that is blacklisted from mass-assignment" do before do diff --git a/spec/shoulda/active_record/allow_value_matcher_spec.rb b/spec/shoulda/active_model/allow_value_matcher_spec.rb similarity index 96% rename from spec/shoulda/active_record/allow_value_matcher_spec.rb rename to spec/shoulda/active_model/allow_value_matcher_spec.rb index e718475b..092100dc 100644 --- a/spec/shoulda/active_record/allow_value_matcher_spec.rb +++ b/spec/shoulda/active_model/allow_value_matcher_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Shoulda::Matchers::ActiveRecord::AllowValueMatcher do +describe Shoulda::Matchers::ActiveModel::AllowValueMatcher do context "an attribute with a format validation" do before do diff --git a/spec/shoulda/active_record/ensure_inclusion_of_matcher_spec.rb b/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb similarity index 96% rename from spec/shoulda/active_record/ensure_inclusion_of_matcher_spec.rb rename to spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb index 3f10019c..cb7e736c 100644 --- a/spec/shoulda/active_record/ensure_inclusion_of_matcher_spec.rb +++ b/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Shoulda::Matchers::ActiveRecord::EnsureInclusionOfMatcher do +describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do context "an attribute which must be included in a range" do before do diff --git a/spec/shoulda/active_record/ensure_length_of_matcher_spec.rb b/spec/shoulda/active_model/ensure_length_of_matcher_spec.rb similarity index 98% rename from spec/shoulda/active_record/ensure_length_of_matcher_spec.rb rename to spec/shoulda/active_model/ensure_length_of_matcher_spec.rb index 81d009f0..dd11ce08 100644 --- a/spec/shoulda/active_record/ensure_length_of_matcher_spec.rb +++ b/spec/shoulda/active_model/ensure_length_of_matcher_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Shoulda::Matchers::ActiveRecord::EnsureLengthOfMatcher do +describe Shoulda::Matchers::ActiveModel::EnsureLengthOfMatcher do context "an attribute with a non-zero minimum length validation" do before do diff --git a/spec/shoulda/active_record/validate_acceptance_of_matcher_spec.rb b/spec/shoulda/active_model/validate_acceptance_of_matcher_spec.rb similarity index 93% rename from spec/shoulda/active_record/validate_acceptance_of_matcher_spec.rb rename to spec/shoulda/active_model/validate_acceptance_of_matcher_spec.rb index 1f93d699..8a14cb4a 100644 --- a/spec/shoulda/active_record/validate_acceptance_of_matcher_spec.rb +++ b/spec/shoulda/active_model/validate_acceptance_of_matcher_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Shoulda::Matchers::ActiveRecord::ValidateAcceptanceOfMatcher do +describe Shoulda::Matchers::ActiveModel::ValidateAcceptanceOfMatcher do context "an attribute which must be accepted" do before do diff --git a/spec/shoulda/active_record/validate_format_of_matcher_spec.rb b/spec/shoulda/active_model/validate_format_of_matcher_spec.rb similarity index 94% rename from spec/shoulda/active_record/validate_format_of_matcher_spec.rb rename to spec/shoulda/active_model/validate_format_of_matcher_spec.rb index fabac847..8a2bf44e 100644 --- a/spec/shoulda/active_record/validate_format_of_matcher_spec.rb +++ b/spec/shoulda/active_model/validate_format_of_matcher_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Shoulda::Matchers::ActiveRecord::ValidateFormatOfMatcher do +describe Shoulda::Matchers::ActiveModel::ValidateFormatOfMatcher do context "a postal code" do before do diff --git a/spec/shoulda/active_record/validate_numericality_of_matcher_spec.rb b/spec/shoulda/active_model/validate_numericality_of_matcher_spec.rb similarity index 94% rename from spec/shoulda/active_record/validate_numericality_of_matcher_spec.rb rename to spec/shoulda/active_model/validate_numericality_of_matcher_spec.rb index d39525cd..f39f7410 100644 --- a/spec/shoulda/active_record/validate_numericality_of_matcher_spec.rb +++ b/spec/shoulda/active_model/validate_numericality_of_matcher_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Shoulda::Matchers::ActiveRecord::ValidateNumericalityOfMatcher do +describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher do context "a numeric attribute" do before do diff --git a/spec/shoulda/active_record/validate_presence_of_matcher_spec.rb b/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb similarity index 97% rename from spec/shoulda/active_record/validate_presence_of_matcher_spec.rb rename to spec/shoulda/active_model/validate_presence_of_matcher_spec.rb index 7fd6f792..0282e216 100644 --- a/spec/shoulda/active_record/validate_presence_of_matcher_spec.rb +++ b/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Shoulda::Matchers::ActiveRecord::ValidatePresenceOfMatcher do +describe Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher do context "a required attribute" do before do diff --git a/spec/shoulda/active_record/validate_uniqueness_of_matcher_spec.rb b/spec/shoulda/active_model/validate_uniqueness_of_matcher_spec.rb similarity index 98% rename from spec/shoulda/active_record/validate_uniqueness_of_matcher_spec.rb rename to spec/shoulda/active_model/validate_uniqueness_of_matcher_spec.rb index 2d3ffda4..910f3c63 100644 --- a/spec/shoulda/active_record/validate_uniqueness_of_matcher_spec.rb +++ b/spec/shoulda/active_model/validate_uniqueness_of_matcher_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Shoulda::Matchers::ActiveRecord::ValidateUniquenessOfMatcher do +describe Shoulda::Matchers::ActiveModel::ValidateUniquenessOfMatcher do context "a unique attribute" do before do