Organizando plugins e gems.

This commit is contained in:
2009-07-16 11:51:47 -03:00
parent 4e22c87074
commit dcfc38eb09
506 changed files with 10538 additions and 45562 deletions

View File

@@ -0,0 +1,68 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class AllowMassAssignmentOfMatcherTest < Test::Unit::TestCase # :nodoc:
context "an attribute that is blacklisted from mass-assignment" do
setup do
define_model :example, :attr => :string do
attr_protected :attr
end
@model = Example.new
end
should "reject being mass-assignable" do
assert_rejects allow_mass_assignment_of(:attr), @model
end
end
context "an attribute that is not whitelisted for mass-assignment" do
setup do
define_model :example, :attr => :string, :other => :string do
attr_accessible :other
end
@model = Example.new
end
should "reject being mass-assignable" do
assert_rejects allow_mass_assignment_of(:attr), @model
end
end
context "an attribute that is whitelisted for mass-assignment" do
setup do
define_model :example, :attr => :string do
attr_accessible :attr
end
@model = Example.new
end
should "accept being mass-assignable" do
assert_accepts allow_mass_assignment_of(:attr), @model
end
end
context "an attribute not included in the mass-assignment blacklist" do
setup do
define_model :example, :attr => :string, :other => :string do
attr_protected :other
end
@model = Example.new
end
should "accept being mass-assignable" do
assert_accepts allow_mass_assignment_of(:attr), @model
end
end
context "an attribute on a class with no protected attributes" do
setup do
define_model :example, :attr => :string
@model = Example.new
end
should "accept being mass-assignable" do
assert_accepts allow_mass_assignment_of(:attr), @model
end
end
end

View File

@@ -0,0 +1,41 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class AllowValueMatcherTest < Test::Unit::TestCase # :nodoc:
context "an attribute with a format validation" do
setup do
define_model :example, :attr => :string do
validates_format_of :attr, :with => /abc/
end
@model = Example.new
end
should "allow a good value" do
assert_accepts allow_value("abcde").for(:attr), @model
end
should "not allow a bad value" do
assert_rejects allow_value("xyz").for(:attr), @model
end
end
context "an attribute with a format validation and a custom message" do
setup do
define_model :example, :attr => :string do
validates_format_of :attr, :with => /abc/, :message => 'bad value'
end
@model = Example.new
end
should "allow a good value" do
assert_accepts allow_value('abcde').for(:attr).with_message(/bad/),
@model
end
should "not allow a bad value" do
assert_rejects allow_value('xyz').for(:attr).with_message(/bad/),
@model
end
end
end

View File

@@ -0,0 +1,258 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class AssociationMatcherTest < Test::Unit::TestCase # :nodoc:
context "belong_to" do
setup do
@matcher = belong_to(:parent)
end
should "accept a good association with the default foreign key" do
define_model :parent
define_model :child, :parent_id => :integer do
belongs_to :parent
end
assert_accepts @matcher, Child.new
end
should "reject a nonexistent association" do
define_model :child
assert_rejects @matcher, Child.new
end
should "reject an association of the wrong type" do
define_model :parent, :child_id => :integer
child_class = define_model :child do
has_one :parent
end
assert_rejects @matcher, Child.new
end
should "reject an association that has a nonexistent foreign key" do
define_model :parent
define_model :child do
belongs_to :parent
end
assert_rejects @matcher, Child.new
end
should "accept an association with an existing custom foreign key" do
define_model :parent
define_model :child, :guardian_id => :integer do
belongs_to :parent, :foreign_key => 'guardian_id'
end
assert_accepts @matcher, Child.new
end
should "accept a polymorphic association" do
define_model :child, :parent_type => :string,
:parent_id => :integer do
belongs_to :parent, :polymorphic => true
end
assert_accepts @matcher, Child.new
end
should "accept an association with a valid :dependent option" do
define_model :parent
define_model :child, :parent_id => :integer do
belongs_to :parent, :dependent => :destroy
end
assert_accepts @matcher.dependent(:destroy), Child.new
end
should "reject an association with a bad :dependent option" do
define_model :parent
define_model :child, :parent_id => :integer do
belongs_to :parent
end
assert_rejects @matcher.dependent(:destroy), Child.new
end
end
context "have_many" do
setup do
@matcher = have_many(:children)
end
should "accept a valid association without any options" do
define_model :child, :parent_id => :integer
define_model :parent do
has_many :children
end
assert_accepts @matcher, Parent.new
end
should "accept a valid association with a :through option" do
define_model :child
define_model :conception, :child_id => :integer,
:parent_id => :integer do
belongs_to :child
end
define_model :parent do
has_many :conceptions
has_many :children, :through => :conceptions
end
assert_accepts @matcher, Parent.new
end
should "accept a valid association with an :as option" do
define_model :child, :guardian_type => :string,
:guardian_id => :integer
define_model :parent do
has_many :children, :as => :guardian
end
assert_accepts @matcher, Parent.new
end
should "reject an association that has a nonexistent foreign key" do
define_model :child
define_model :parent do
has_many :children
end
assert_rejects @matcher, Parent.new
end
should "reject an association with a bad :as option" do
define_model :child, :caretaker_type => :string,
:caretaker_id => :integer
define_model :parent do
has_many :children, :as => :guardian
end
assert_rejects @matcher, Parent.new
end
should "reject an association that has a bad :through option" do
define_model :child, :parent_id => :integer
define_model :parent do
has_many :children
end
assert_rejects @matcher.through(:conceptions), Parent.new
end
should "reject an association that has the wrong :through option" do
define_model :child
define_model :conception, :child_id => :integer,
:parent_id => :integer do
belongs_to :child
end
define_model :parent do
has_many :conceptions
has_many :children, :through => :conceptions
end
assert_rejects @matcher.through(:relationships), Parent.new
end
should "accept an association with a valid :dependent option" do
define_model :child, :parent_id => :integer
define_model :parent do
has_many :children, :dependent => :destroy
end
assert_accepts @matcher.dependent(:destroy), Parent.new
end
should "reject an association with a bad :dependent option" do
define_model :child, :parent_id => :integer
define_model :parent do
has_many :children
end
assert_rejects @matcher.dependent(:destroy), Parent.new
end
end
context "have_one" do
setup do
@matcher = have_one(:profile)
end
should "accept a valid association without any options" do
define_model :profile, :person_id => :integer
define_model :person do
has_one :profile
end
assert_accepts @matcher, Person.new
end
should "accept a valid association with an :as option" do
define_model :profile, :profilable_id => :integer,
:profilable_type => :string
define_model :person do
has_one :profile, :as => :profilable
end
assert_accepts @matcher, Person.new
end
should "reject an association that has a nonexistent foreign key" do
define_model :profile
define_model :person do
has_one :profile
end
assert_rejects @matcher, Person.new
end
should "reject an association with a bad :as option" do
define_model :profile, :profilable_id => :integer,
:profilable_type => :string
define_model :person do
has_one :profile, :as => :describable
end
assert_rejects @matcher, Person.new
end
should "accept an association with a valid :dependent option" do
define_model :profile, :person_id => :integer
define_model :person do
has_one :profile, :dependent => :destroy
end
assert_accepts @matcher.dependent(:destroy), Person.new
end
should "reject an association with a bad :dependent option" do
define_model :profile, :person_id => :integer
define_model :person do
has_one :profile
end
assert_rejects @matcher.dependent(:destroy), Person.new
end
end
context "have_and_belong_to_many" do
setup do
@matcher = have_and_belong_to_many(:relatives)
end
should "accept a valid association" do
define_model :relatives
define_model :person do
has_and_belongs_to_many :relatives
end
define_model :people_relative, :person_id => :integer,
:relative_id => :integer
assert_accepts @matcher, Person.new
end
should "reject a nonexistent association" do
define_model :relatives
define_model :person
define_model :people_relative, :person_id => :integer,
:relative_id => :integer
assert_rejects @matcher, Person.new
end
should "reject an association with a nonexistent join table" do
define_model :relatives
define_model :person do
has_and_belongs_to_many :relatives
end
assert_rejects @matcher, Person.new
end
should "reject an association of the wrong type" do
define_model :relatives, :person_id => :integer
define_model :person do
has_many :relatives
end
assert_rejects @matcher, Person.new
end
end
end

View File

@@ -0,0 +1,80 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class EnsureInclusionOfMatcherTest < Test::Unit::TestCase # :nodoc:
context "an attribute which must be included in a range" do
setup do
@model = define_model(:example, :attr => :integer) do
validates_inclusion_of :attr, :in => 2..5
end.new
end
should "accept ensuring the correct range" do
assert_accepts ensure_inclusion_of(:attr).in_range(2..5), @model
end
should "reject ensuring a lower minimum value" do
assert_rejects ensure_inclusion_of(:attr).in_range(1..5), @model
end
should "reject ensuring a higher minimum value" do
assert_rejects ensure_inclusion_of(:attr).in_range(3..5), @model
end
should "reject ensuring a lower maximum value" do
assert_rejects ensure_inclusion_of(:attr).in_range(2..4), @model
end
should "reject ensuring a higher maximum value" do
assert_rejects ensure_inclusion_of(:attr).in_range(2..6), @model
end
should "not override the default message with a blank" do
assert_accepts ensure_inclusion_of(:attr).
in_range(2..5).
with_message(nil),
@model
end
end
context "an attribute with a custom ranged value validation" do
setup do
@model = define_model(:example, :attr => :string) do
validates_inclusion_of :attr, :in => 2..4, :message => 'not good'
end.new
end
should "accept ensuring the correct range" do
assert_accepts ensure_inclusion_of(:attr).
in_range(2..4).
with_message(/not good/),
@model
end
end
context "an attribute with custom range validations" do
setup do
define_model :example, :attr => :integer do
def validate
if attr < 2
errors.add(:attr, 'too low')
elsif attr > 5
errors.add(:attr, 'too high')
end
end
end
@model = Example.new
end
should "accept ensuring the correct range and messages" do
assert_accepts ensure_inclusion_of(:attr).
in_range(2..5).
with_low_message(/low/).
with_high_message(/high/),
@model
end
end
end

View File

@@ -0,0 +1,158 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class EnsureLengthOfMatcher < Test::Unit::TestCase # :nodoc:
context "an attribute with a non-zero minimum length validation" do
setup do
@model = define_model(:example, :attr => :string) do
validates_length_of :attr, :minimum => 4
end.new
end
should "accept ensuring the correct minimum length" do
assert_accepts ensure_length_of(:attr).is_at_least(4), @model
end
should "reject ensuring a lower minimum length with any message" do
assert_rejects ensure_length_of(:attr).
is_at_least(3).
with_short_message(/.*/),
@model
end
should "reject ensuring a higher minimum length with any message" do
assert_rejects ensure_length_of(:attr).
is_at_least(5).
with_short_message(/.*/),
@model
end
should "not override the default message with a blank" do
assert_accepts ensure_length_of(:attr).
is_at_least(4).
with_short_message(nil),
@model
end
end
context "an attribute with a minimum length validation of 0" do
setup do
@model = define_model(:example, :attr => :string) do
validates_length_of :attr, :minimum => 0
end.new
end
should "accept ensuring the correct minimum length" do
assert_accepts ensure_length_of(:attr).is_at_least(0), @model
end
end
context "an attribute with a maximum length" do
setup do
@model = define_model(:example, :attr => :string) do
validates_length_of :attr, :maximum => 4
end.new
end
should "accept ensuring the correct maximum length" do
assert_accepts ensure_length_of(:attr).is_at_most(4), @model
end
should "reject ensuring a lower maximum length with any message" do
assert_rejects ensure_length_of(:attr).
is_at_most(3).
with_long_message(/.*/),
@model
end
should "reject ensuring a higher maximum length with any message" do
assert_rejects ensure_length_of(:attr).
is_at_most(5).
with_long_message(/.*/),
@model
end
should "not override the default message with a blank" do
assert_accepts ensure_length_of(:attr).
is_at_most(4).
with_long_message(nil),
@model
end
end
context "an attribute with a required exact length" do
setup do
@model = define_model(:example, :attr => :string) do
validates_length_of :attr, :is => 4
end.new
end
should "accept ensuring the correct length" do
assert_accepts ensure_length_of(:attr).is_equal_to(4), @model
end
should "reject ensuring a lower maximum length with any message" do
assert_rejects ensure_length_of(:attr).
is_equal_to(3).
with_message(/.*/),
@model
end
should "reject ensuring a higher maximum length with any message" do
assert_rejects ensure_length_of(:attr).
is_equal_to(5).
with_message(/.*/),
@model
end
should "not override the default message with a blank" do
assert_accepts ensure_length_of(:attr).
is_equal_to(4).
with_message(nil),
@model
end
end
context "an attribute with a custom minimum length validation" do
setup do
@model = define_model(:example, :attr => :string) do
validates_length_of :attr, :minimum => 4, :too_short => 'short'
end.new
end
should "accept ensuring the correct minimum length" do
assert_accepts ensure_length_of(:attr).
is_at_least(4).
with_short_message(/short/),
@model
end
end
context "an attribute with a custom maximum length validation" do
setup do
@model = define_model(:example, :attr => :string) do
validates_length_of :attr, :maximum => 4, :too_long => 'long'
end.new
end
should "accept ensuring the correct minimum length" do
assert_accepts ensure_length_of(:attr).
is_at_most(4).
with_long_message(/long/),
@model
end
end
context "an attribute without a length validation" do
setup do
@model = define_model(:example, :attr => :string).new
end
should "reject ensuring a minimum length" do
assert_rejects ensure_length_of(:attr).is_at_least(1), @model
end
end
end

View File

@@ -0,0 +1,169 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class HaveDbColumnMatcherTest < Test::Unit::TestCase # :nodoc:
context "have_db_column" do
setup do
@matcher = have_db_column(:nickname)
end
should "accept an existing database column" do
create_table 'superheros' do |table|
table.string :nickname
end
define_model_class 'Superhero'
assert_accepts @matcher, Superhero.new
end
should "reject a nonexistent database column" do
define_model :superhero
assert_rejects @matcher, Superhero.new
end
end
context "have_db_column of type string" do
setup do
@matcher = have_db_column(:nickname).of_type(:string)
end
should "accept a column of correct type" do
create_table 'superheros' do |table|
table.string :nickname
end
define_model_class 'Superhero'
assert_accepts @matcher, Superhero.new
end
should "reject a nonexistent database column" do
define_model :superhero
assert_rejects @matcher, Superhero.new
end
should "reject a column of wrong type" do
create_table 'superheros' do |table|
table.integer :nickname
end
define_model_class 'Superhero'
assert_rejects @matcher, Superhero.new
end
end
context "have_db_column with precision option" do
setup do
@matcher = have_db_column(:salary).with_options(:precision => 5)
end
should "accept a column of correct precision" do
create_table 'superheros' do |table|
table.decimal :salary, :precision => 5
end
define_model_class 'Superhero'
assert_accepts @matcher, Superhero.new
end
should "reject a column of wrong precision" do
create_table 'superheros' do |table|
table.decimal :salary, :precision => 15
end
define_model_class 'Superhero'
assert_rejects @matcher, Superhero.new
end
end
context "have_db_column with limit option" do
setup do
@matcher = have_db_column(:email).
of_type(:string).
with_options(:limit => 255)
end
should "accept a column of correct limit" do
create_table 'superheros' do |table|
table.string :email, :limit => 255
end
define_model_class 'Superhero'
assert_accepts @matcher, Superhero.new
end
should "reject a column of wrong limit" do
create_table 'superheros' do |table|
table.string :email, :limit => 500
end
define_model_class 'Superhero'
assert_rejects @matcher, Superhero.new
end
end
context "have_db_column with default option" do
setup do
@matcher = have_db_column(:admin).
of_type(:boolean).
with_options(:default => false)
end
should "accept a column of correct default" do
create_table 'superheros' do |table|
table.boolean :admin, :default => false
end
define_model_class 'Superhero'
assert_accepts @matcher, Superhero.new
end
should "reject a column of wrong default" do
create_table 'superheros' do |table|
table.boolean :admin, :default => true
end
define_model_class 'Superhero'
assert_rejects @matcher, Superhero.new
end
end
context "have_db_column with null option" do
setup do
@matcher = have_db_column(:admin).
of_type(:boolean).
with_options(:null => false)
end
should "accept a column of correct null" do
create_table 'superheros' do |table|
table.boolean :admin, :null => false
end
define_model_class 'Superhero'
assert_accepts @matcher, Superhero.new
end
should "reject a column of wrong null" do
create_table 'superheros' do |table|
table.boolean :admin, :null => true
end
define_model_class 'Superhero'
assert_rejects @matcher, Superhero.new
end
end
context "have_db_column with scale option" do
setup do
@matcher = have_db_column(:salary).
of_type(:decimal).
with_options(:scale => 2)
end
should "accept a column of correct scale" do
create_table 'superheros' do |table|
table.decimal :salary, :precision => 10, :scale => 2
end
define_model_class 'Superhero'
assert_accepts @matcher, Superhero.new
end
should "reject a column of wrong scale" do
create_table 'superheros' do |table|
table.decimal :salary, :precision => 10, :scale => 4
end
define_model_class 'Superhero'
assert_rejects @matcher, Superhero.new
end
end
end

View File

@@ -0,0 +1,74 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class HaveIndexMatcherTest < Test::Unit::TestCase # :nodoc:
context "have_index" do
setup do
@matcher = have_index(:age)
end
should "accept an existing index" do
db_connection = create_table 'superheros' do |table|
table.integer :age
end
db_connection.add_index :superheros, :age
define_model_class 'Superhero'
assert_accepts @matcher, Superhero.new
end
should "reject a nonexistent index" do
define_model :superhero
assert_rejects @matcher, Superhero.new
end
end
context "have_index with unique option" do
setup do
@matcher = have_index(:ssn).unique(true)
end
should "accept an index of correct unique" do
db_connection = create_table 'superheros' do |table|
table.integer :ssn
end
db_connection.add_index :superheros, :ssn, :unique => true
define_model_class 'Superhero'
assert_accepts @matcher, Superhero.new
end
should "reject an index of wrong unique" do
db_connection = create_table 'superheros' do |table|
table.integer :ssn
end
db_connection.add_index :superheros, :ssn, :unique => false
define_model_class 'Superhero'
assert_rejects @matcher, Superhero.new
end
end
context "have_index on multiple columns" do
setup do
@matcher = have_index([:geocodable_type, :geocodable_id])
end
should "accept an existing index" do
db_connection = create_table 'geocodings' do |table|
table.integer :geocodable_id
table.string :geocodable_type
end
db_connection.add_index :geocodings, [:geocodable_type, :geocodable_id]
define_model_class 'Geocoding'
assert_accepts @matcher, Geocoding.new
end
should "reject a nonexistant index" do
db_connection = create_table 'geocodings' do |table|
table.integer :geocodable_id
table.string :geocodable_type
end
define_model_class 'Geocoding'
assert_rejects @matcher, Geocoding.new
end
end
end

View File

@@ -0,0 +1,65 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class HaveNamedScopeMatcherTest < Test::Unit::TestCase # :nodoc:
context "an attribute with a named scope" do
setup do
define_model :example, :attr => :string do
named_scope :xyz, lambda {|n|
{ :order => :attr }
}
end
@model = Example.new
end
should "accept having a scope with the correct signature" do
assert_accepts have_named_scope("xyz(1)"), @model
end
should "accept having a scope with the correct signature and find options" do
assert_accepts have_named_scope("xyz(1)").finding(:order => :attr), @model
end
should "reject having a scope with incorrect find options" do
assert_rejects have_named_scope("xyz(1)").
finding(:order => 'attr DESC'),
@model
end
should "reject having a scope with another name" do
assert_rejects have_named_scope("abc(1)"), @model
end
end
should "evaluate the scope in the correct context" do
define_model :example, :attr => :string do
named_scope :xyz, lambda {|n|
{ :order => n }
}
end
model = Example.new
@order = :attr
assert_accepts have_named_scope("xyz(@order)").
finding(:order => @order).
in_context(self),
model
end
context "a method that does not return a scope" do
setup do
klass = Class.new
klass.class_eval do
def self.xyz
'xyz'
end
end
@model = klass.new
end
should "reject having a named scope with that name" do
assert_rejects have_named_scope(:xyz), @model
end
end
end

View File

@@ -0,0 +1,29 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class HaveReadonlyAttributesMatcherTest < Test::Unit::TestCase # :nodoc:
context "an attribute that cannot be set after being saved" do
setup do
define_model :example, :attr => :string do
attr_readonly :attr
end
@model = Example.new
end
should "accept being read-only" do
assert_accepts have_readonly_attribute(:attr), @model
end
end
context "an attribute that can be set after being saved" do
setup do
define_model :example, :attr => :string
@model = Example.new
end
should "accept being read-only" do
assert_rejects have_readonly_attribute(:attr), @model
end
end
end

View File

@@ -0,0 +1,44 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class ValidateAcceptanceOfMatcherTest < Test::Unit::TestCase # :nodoc:
context "an attribute which must be accepted" do
setup do
@model = define_model(:example) do
validates_acceptance_of :attr
end.new
end
should "require that attribute to be accepted" do
assert_accepts validate_acceptance_of(:attr), @model
end
should "not overwrite the default message with nil" do
assert_accepts validate_acceptance_of(:attr).with_message(nil), @model
end
end
context "an attribute that does not need to be accepted" do
setup do
@model = define_model(:example, :attr => :string).new
end
should "not require that attribute to be accepted" do
assert_rejects validate_acceptance_of(:attr), @model
end
end
context "an attribute which must be accepted with a custom message" do
setup do
@model = define_model(:example) do
validates_acceptance_of :attr, :message => 'custom'
end.new
end
should "require that attribute to be accepted with that message" do
assert_accepts validate_acceptance_of(:attr).with_message(/custom/),
@model
end
end
end

View File

@@ -0,0 +1,52 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class ValidateNumericalityOfMatcherTest < Test::Unit::TestCase # :nodoc:
context "a numeric attribute" do
setup do
define_model :example, :attr => :string do
validates_numericality_of :attr
end
@model = Example.new
end
should "only allow numeric values for that attribute" do
assert_accepts validate_numericality_of(:attr), @model
end
should "not override the default message with a blank" do
assert_accepts validate_numericality_of(:attr).with_message(nil),
@model
end
end
context "a numeric attribute with a custom validation message" do
setup do
define_model :example, :attr => :string do
validates_numericality_of :attr, :message => 'custom'
end
@model = Example.new
end
should "only allow numeric values for that attribute with that message" do
assert_accepts validate_numericality_of(:attr).
with_message(/custom/),
@model
end
should "not allow numeric values for that attribute with another message" do
assert_rejects validate_numericality_of(:attr), @model
end
end
context "a non-numeric attribute" do
setup do
@model = define_model(:example, :attr => :string).new
end
should "not only allow numeric values for that attribute" do
assert_rejects validate_numericality_of(:attr), @model
end
end
end

View File

@@ -0,0 +1,86 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class ValidatePresenceOfMatcherTest < Test::Unit::TestCase # :nodoc:
context "a required attribute" do
setup do
define_model :example, :attr => :string do
validates_presence_of :attr
end
@model = Example.new
end
should "require a value" do
assert_accepts validate_presence_of(:attr), @model
end
should "not override the default message with a blank" do
assert_accepts validate_presence_of(:attr).with_message(nil), @model
end
end
context "an optional attribute" do
setup do
@model = define_model(:example, :attr => :string).new
end
should "not require a value" do
assert_rejects validate_presence_of(:attr), @model
end
end
context "a required has_many association" do
setup do
define_model :child
@model = define_model :parent do
has_many :children
validates_presence_of :children
end.new
end
should "require the attribute to be set" do
assert_accepts validate_presence_of(:children), @model
end
end
context "an optional has_many association" do
setup do
define_model :child
@model = define_model :parent do
has_many :children
end.new
end
should "not require the attribute to be set" do
assert_rejects validate_presence_of(:children), @model
end
end
context "a required has_and_belongs_to_many association" do
setup do
define_model :child
@model = define_model :parent do
has_and_belongs_to_many :children
validates_presence_of :children
end.new
end
should "require the attribute to be set" do
assert_accepts validate_presence_of(:children), @model
end
end
context "an optional has_and_belongs_to_many association" do
setup do
define_model :child
@model = define_model :parent do
has_and_belongs_to_many :children
end.new
end
should "not require the attribute to be set" do
assert_rejects validate_presence_of(:children), @model
end
end
end

View File

@@ -0,0 +1,147 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class ValidateUniquenessOfMatcherTest < Test::Unit::TestCase # :nodoc:
context "a unique attribute" do
setup do
@model = define_model(:example, :attr => :string,
:other => :integer) do
validates_uniqueness_of :attr
end.new
end
context "with an existing value" do
setup do
@existing = Example.create!(:attr => 'value', :other => 1)
end
should "require a unique value for that attribute" do
assert_accepts validate_uniqueness_of(:attr), @model
end
should "pass when the subject is an existing record" do
assert_accepts validate_uniqueness_of(:attr), @existing
end
should "fail when a scope is specified" do
assert_rejects validate_uniqueness_of(:attr).scoped_to(:other), @model
end
end
context "without an existing value" do
setup do
assert_nil Example.find(:first)
@matcher = validate_uniqueness_of(:attr)
end
should "fail to require a unique value" do
assert_rejects @matcher, @model
end
should "alert the tester that an existing value is not present" do
@matcher.matches?(@model)
assert @matcher.negative_failure_message =~ /^Can't find first .*/
end
end
end
context "a unique attribute with a custom error and an existing value" do
setup do
@model = define_model(:example, :attr => :string) do
validates_uniqueness_of :attr, :message => 'Bad value'
end.new
Example.create!
end
should "fail when checking the default message" do
assert_rejects validate_uniqueness_of(:attr), @model
end
should "fail when checking a message that doesn't match" do
assert_rejects validate_uniqueness_of(:attr).with_message(/abc/i), @model
end
should "pass when checking a message that matches" do
assert_accepts validate_uniqueness_of(:attr).with_message(/bad/i), @model
end
end
context "a scoped unique attribute with an existing value" do
setup do
@model = define_model(:example, :attr => :string,
:scope1 => :integer,
:scope2 => :integer) do
validates_uniqueness_of :attr, :scope => [:scope1, :scope2]
end.new
@existing = Example.create!(:attr => 'value', :scope1 => 1, :scope2 => 2)
end
should "pass when the correct scope is specified" do
assert_accepts validate_uniqueness_of(:attr).scoped_to(:scope1, :scope2),
@model
end
should "pass when the subject is an existing record" do
assert_accepts validate_uniqueness_of(:attr).scoped_to(:scope1, :scope2),
@existing
end
should "fail when a different scope is specified" do
assert_rejects validate_uniqueness_of(:attr).scoped_to(:scope1), @model
end
should "fail when no scope is specified" do
assert_rejects validate_uniqueness_of(:attr), @model
end
should "fail when a non-existent attribute is specified as a scope" do
assert_rejects validate_uniqueness_of(:attr).scoped_to(:fake), @model
end
end
context "a non-unique attribute with an existing value" do
setup do
@model = define_model(:example, :attr => :string).new
Example.create!(:attr => 'value')
end
should "not require a unique value for that attribute" do
assert_rejects validate_uniqueness_of(:attr), @model
end
end
context "a case sensitive unique attribute with an existing value" do
setup do
@model = define_model(:example, :attr => :string) do
validates_uniqueness_of :attr, :case_sensitive => true
end.new
Example.create!(:attr => 'value')
end
should "not require a unique, case-insensitive value for that attribute" do
assert_rejects validate_uniqueness_of(:attr).case_insensitive, @model
end
should "require a unique, case-sensitive value for that attribute" do
assert_accepts validate_uniqueness_of(:attr), @model
end
end
context "a case sensitive unique integer attribute with an existing value" do
setup do
@model = define_model(:example, :attr => :integer) do
validates_uniqueness_of :attr, :case_sensitive => true
end.new
Example.create!(:attr => 'value')
end
should "require a unique, case-insensitive value for that attribute" do
assert_accepts validate_uniqueness_of(:attr).case_insensitive, @model
end
should "require a unique, case-sensitive value for that attribute" do
assert_accepts validate_uniqueness_of(:attr), @model
end
end
end

View File

@@ -0,0 +1,35 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class AssignToMatcherTest < Test::Unit::TestCase # :nodoc:
context "a controller that assigns to an instance variable" do
setup do
@controller = build_response { @var = 'value' }
end
should "accept assigning to that variable" do
assert_accepts assign_to(:var), @controller
end
should "accept assigning to that variable with the correct class" do
assert_accepts assign_to(:var).with_kind_of(String), @controller
end
should "reject assigning to that variable with another class" do
assert_rejects assign_to(:var).with_kind_of(Fixnum), @controller
end
should "accept assigning the correct value to that variable" do
assert_accepts assign_to(:var).with('value'), @controller
end
should "reject assigning another value to that variable" do
assert_rejects assign_to(:var).with('other'), @controller
end
should "reject assigning to another variable" do
assert_rejects assign_to(:other), @controller
end
end
end

View File

@@ -0,0 +1,32 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class FilterParamMatcherTest < Test::Unit::TestCase # :nodoc:
context "a controller that filters no parameters" do
setup do
@controller = define_controller(:examples).new
end
should "reject filtering any parameter" do
assert_rejects filter_param(:any), @controller
end
end
context "a controller that filters a parameter" do
setup do
@controller = define_controller :examples do
filter_parameter_logging :password
end.new
end
should "accept filtering that parameter" do
assert_accepts filter_param(:password), @controller
end
should "reject filtering another parameter" do
assert_rejects filter_param(:other), @controller
end
end
end

View File

@@ -0,0 +1,33 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class RenderWithLayoutMatcherTest < Test::Unit::TestCase # :nodoc:
context "a controller that renders with a layout" do
setup do
@controller = build_response { render :layout => 'wide' }
end
should "accept rendering with any layout" do
assert_accepts render_with_layout, @controller
end
should "accept rendering with that layout" do
assert_accepts render_with_layout(:wide), @controller
end
should "reject rendering with another layout" do
assert_rejects render_with_layout(:other), @controller
end
end
context "a controller that renders without a layout" do
setup do
@controller = build_response { render :layout => false }
end
should "reject rendering with a layout" do
assert_rejects render_with_layout, @controller
end
end
end

View File

@@ -0,0 +1,27 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class RespondWithContentTypeMatcherTest < Test::Unit::TestCase # :nodoc:
context "a controller responding with content type :xml" do
setup do
@controller = build_response { render :xml => { :user => "thoughtbot" }.to_xml }
end
should "accept responding with content type :xml" do
assert_accepts respond_with_content_type(:xml), @controller
end
should "accept responding with content type 'application/xml'" do
assert_accepts respond_with_content_type('application/xml'), @controller
end
should "accept responding with content type /xml/" do
assert_accepts respond_with_content_type(/xml/), @controller
end
should "reject responding with another content type" do
assert_rejects respond_with_content_type(:json), @controller
end
end
end

View File

@@ -0,0 +1,106 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class RespondWithMatcherTest < Test::Unit::TestCase # :nodoc:
context "a controller responding with success" do
setup do
@controller = build_response { render :text => "text", :status => 200 }
end
should "accept responding with 200" do
assert_accepts respond_with(200), @controller
end
should "accept responding with :success" do
assert_accepts respond_with(:success), @controller
end
should "reject responding with another status" do
assert_rejects respond_with(:error), @controller
end
end
context "a controller responding with redirect" do
setup do
@controller = build_response { render :text => "text", :status => 301 }
end
should "accept responding with 301" do
assert_accepts respond_with(301), @controller
end
should "accept responding with :redirect" do
assert_accepts respond_with(:redirect), @controller
end
should "reject responding with another status" do
assert_rejects respond_with(:error), @controller
end
end
context "a controller responding with missing" do
setup do
@controller = build_response { render :text => "text", :status => 404 }
end
should "accept responding with 404" do
assert_accepts respond_with(404), @controller
end
should "accept responding with :missing" do
assert_accepts respond_with(:missing), @controller
end
should "reject responding with another status" do
assert_rejects respond_with(:success), @controller
end
end
context "a controller responding with error" do
setup do
@controller = build_response { render :text => "text", :status => 500 }
end
should "accept responding with 500" do
assert_accepts respond_with(500), @controller
end
should "accept responding with :error" do
assert_accepts respond_with(:error), @controller
end
should "reject responding with another status" do
assert_rejects respond_with(:success), @controller
end
end
context "a controller responding with not implemented" do
setup do
@controller = build_response { render :text => "text", :status => 501 }
end
should "accept responding with 501" do
assert_accepts respond_with(501), @controller
end
should "accept responding with :not_implemented" do
assert_accepts respond_with(:not_implemented), @controller
end
should "reject responding with another status" do
assert_rejects respond_with(:success), @controller
end
end
context "a controller raising an error" do
setup do
@controller = build_response { raise RailsError }
end
should "reject responding with any status" do
assert_rejects respond_with(:success), @controller
end
end
end

View File

@@ -0,0 +1,58 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class RouteToMatcherTest < Test::Unit::TestCase # :nodoc:
context "given a controller with a defined route" do
setup do
@controller = define_controller('Examples').new
define_routes do |map|
map.connect 'examples/:id', :controller => 'examples',
:action => 'example'
end
end
should "accept routing the correct path to the correct parameters" do
assert_accepts route(:get, '/examples/1').
to(:action => 'example', :id => '1'),
@controller
end
should "accept a symbol controller" do
assert_accepts route(:get, '/examples/1').
to(:controller => :examples,
:action => 'example',
:id => '1'),
self
end
should "accept a symbol action" do
assert_accepts route(:get, '/examples/1').
to(:action => :example, :id => '1'),
@controller
end
should "accept a non-string parameter" do
assert_accepts route(:get, '/examples/1').
to(:action => 'example', :id => 1),
@controller
end
should "reject an undefined route" do
assert_rejects route(:get, '/bad_route').to(:var => 'value'), @controller
end
should "reject a route for another controller" do
@other = define_controller('Other').new
assert_rejects route(:get, '/examples/1').
to(:action => 'example', :id => '1'),
@other
end
should "reject a route for different parameters" do
assert_rejects route(:get, '/examples/1').
to(:action => 'other', :id => '1'),
@controller
end
end
end

View File

@@ -0,0 +1,31 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class SetSessionMatcherTest < Test::Unit::TestCase # :nodoc:
context "a controller that sets a session variable" do
setup do
@controller = build_response { session[:var] = 'value' }
end
should "accept assigning to that variable" do
assert_accepts set_session(:var), @controller
end
should "accept assigning the correct value to that variable" do
assert_accepts set_session(:var).to('value'), @controller
end
should "reject assigning another value to that variable" do
assert_rejects set_session(:var).to('other'), @controller
end
should "reject assigning to another variable" do
assert_rejects set_session(:other), @controller
end
should "accept assigning nil to another variable" do
assert_accepts set_session(:other).to(nil), @controller
end
end
end

View File

@@ -0,0 +1,41 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class SetTheFlashMatcherTest < Test::Unit::TestCase # :nodoc:
context "a controller that sets a flash message" do
setup do
@controller = build_response { flash[:notice] = 'value' }
end
should "accept setting any flash message" do
assert_accepts set_the_flash, @controller
end
should "accept setting the exact flash message" do
assert_accepts set_the_flash.to('value'), @controller
end
should "accept setting a matched flash message" do
assert_accepts set_the_flash.to(/value/), @controller
end
should "reject setting a different flash message" do
assert_rejects set_the_flash.to('other'), @controller
end
should "reject setting a different pattern" do
assert_rejects set_the_flash.to(/other/), @controller
end
end
context "a controller that doesn't set a flash message" do
setup do
@controller = build_response
end
should "reject setting any flash message" do
assert_rejects set_the_flash, @controller
end
end
end