Seperate ActiveRecord and ActiveModel related matchers

This commit is contained in:
Markus Schwed 2011-05-06 04:51:12 -07:00 committed by Markus Schwed
parent dc769d3524
commit 2a54fdb28d
25 changed files with 85 additions and 43 deletions

View File

@ -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) }

View File

@ -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

View File

@ -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.
#

View File

@ -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.
#

View File

@ -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
#

View File

@ -1,6 +1,6 @@
module Shoulda # :nodoc:
module Matchers
module ActiveRecord # :nodoc:
module ActiveModel # :nodoc:
# Ensures that the length of the attribute is validated.
#

View File

@ -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|

View File

@ -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.

View File

@ -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.

View File

@ -1,6 +1,6 @@
module Shoulda # :nodoc:
module Matchers
module ActiveRecord # :nodoc:
module ActiveModel # :nodoc:
# Ensure that the attribute is numeric
#

View File

@ -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.

View File

@ -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.
#

View File

@ -1,6 +1,6 @@
module Shoulda # :nodoc:
module Matchers
module ActiveRecord # :nodoc:
module ActiveModel # :nodoc:
class ValidationMatcher # :nodoc:

View File

@ -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) }

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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