Documentation

git-svn-id: https://svn.thoughtbot.com/plugins/tb_test_helpers/trunk@42 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
This commit is contained in:
tsaleh 2007-03-14 19:12:11 +00:00
parent 6730ae5f39
commit 0a6da8b218
5 changed files with 223 additions and 212 deletions

1
README
View File

@ -0,0 +1 @@
== Example

View File

@ -1,196 +1,199 @@
class Test::Unit::TestCase
class << self
# Ensures that the model cannot be saved if one of the attributes listed is not present.
# Requires an existing record
def should_require_attributes(*attributes)
klass = self.name.gsub(/Test$/, '').constantize
attributes.each do |attribute|
should "require #{attribute} to be set" do
object = klass.new
assert !object.valid?, "Instance is still valid"
assert object.errors.on(attribute), "No errors found"
assert object.errors.on(attribute).to_a.include?("can't be blank"), "Error message doesn't match"
class Test # :nodoc:
class Unit # :nodoc:
class TestCase
class << self
# Ensures that the model cannot be saved if one of the attributes listed is not present.
# Requires an existing record
def should_require_attributes(*attributes)
klass = self.name.gsub(/Test$/, '').constantize
attributes.each do |attribute|
should "require #{attribute} to be set" do
object = klass.new
assert !object.valid?, "Instance is still valid"
assert object.errors.on(attribute), "No errors found"
assert object.errors.on(attribute).to_a.include?("can't be blank"), "Error message doesn't match"
end
end
end
end
end
# Ensures that the model cannot be saved if one of the attributes listed is not unique.
# Requires an existing record
def should_require_unique_attributes(*attributes)
klass = self.name.gsub(/Test$/, '').constantize
attributes.each do |attribute|
attribute = attribute.to_sym
should "require unique value for #{attribute}" do
assert existing = klass.find(:first), "Can't find first #{klass}"
object = klass.new
object.send(:"#{attribute}=", existing.send(attribute))
assert !object.valid?, "Instance is still valid"
assert object.errors.on(attribute), "No errors found"
assert object.errors.on(attribute).to_a.include?('has already been taken'), "Error message doesn't match"
# Ensures that the model cannot be saved if one of the attributes listed is not unique.
# Requires an existing record
def should_require_unique_attributes(*attributes)
klass = self.name.gsub(/Test$/, '').constantize
attributes.each do |attribute|
attribute = attribute.to_sym
should "require unique value for #{attribute}" do
assert existing = klass.find(:first), "Can't find first #{klass}"
object = klass.new
object.send(:"#{attribute}=", existing.send(attribute))
assert !object.valid?, "Instance is still valid"
assert object.errors.on(attribute), "No errors found"
assert object.errors.on(attribute).to_a.include?('has already been taken'), "Error message doesn't match"
end
end
end
end
end
# Ensures that the attribute cannot be set on update
# Requires an existing record
def should_protect_attributes(*attributes)
klass = self.name.gsub(/Test$/, '').constantize
attributes.each do |attribute|
attribute = attribute.to_sym
should "not allow #{attribute} to be changed by update" do
assert object = klass.find(:first), "Can't find first #{klass}"
value = object[attribute]
assert object.update_attributes({ attribute => 1 }),
"Cannot update #{klass} with { :#{attribute} => 1 }, #{object.errors.full_messages.to_sentence}"
assert object.valid?, "#{klass} isn't valid after changing #{attribute}"
assert_equal value, object[attribute], "Was able to change #{klass}##{attribute}"
# Ensures that the attribute cannot be set on update
# Requires an existing record
def should_protect_attributes(*attributes)
klass = self.name.gsub(/Test$/, '').constantize
attributes.each do |attribute|
attribute = attribute.to_sym
should "not allow #{attribute} to be changed by update" do
assert object = klass.find(:first), "Can't find first #{klass}"
value = object[attribute]
assert object.update_attributes({ attribute => 1 }),
"Cannot update #{klass} with { :#{attribute} => 1 }, #{object.errors.full_messages.to_sentence}"
assert object.valid?, "#{klass} isn't valid after changing #{attribute}"
assert_equal value, object[attribute], "Was able to change #{klass}##{attribute}"
end
end
end
end
end
# Ensures that the attribute cannot be set to the given values
# Requires an existing record
def should_not_allow_values_for(attribute, *bad_values)
klass = self.name.gsub(/Test$/, '').constantize
bad_values.each do |v|
should "not allow #{attribute} to be set to \"#{v}\"" do
assert object = klass.find(:first), "Can't find first #{klass}"
object.send("#{attribute}=", v)
assert !object.save, "Saved #{klass} with #{attribute} set to \"#{v}\""
assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
assert_match(/invalid/, object.errors.on(attribute), "Error set on #{attribute} doesn't include \"invalid\" when set to \"#{v}\"")
# Ensures that the attribute cannot be set to the given values
# Requires an existing record
def should_not_allow_values_for(attribute, *bad_values)
klass = self.name.gsub(/Test$/, '').constantize
bad_values.each do |v|
should "not allow #{attribute} to be set to \"#{v}\"" do
assert object = klass.find(:first), "Can't find first #{klass}"
object.send("#{attribute}=", v)
assert !object.save, "Saved #{klass} with #{attribute} set to \"#{v}\""
assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
assert_match(/invalid/, object.errors.on(attribute), "Error set on #{attribute} doesn't include \"invalid\" when set to \"#{v}\"")
end
end
end
end
end
# Ensures that the attribute can be set to the given values.
# Requires an existing record
def should_allow_values_for(attribute, *good_values)
klass = self.name.gsub(/Test$/, '').constantize
good_values.each do |v|
should "allow #{attribute} to be set to \"#{v}\"" do
assert object = klass.find(:first), "Can't find first #{klass}"
object.send("#{attribute}=", v)
object.save
# assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
assert_no_match(/invalid/, object.errors.on(attribute), "Error set on #{attribute} includes \"invalid\" when set to \"#{v}\"")
# Ensures that the attribute can be set to the given values.
# Requires an existing record
def should_allow_values_for(attribute, *good_values)
klass = self.name.gsub(/Test$/, '').constantize
good_values.each do |v|
should "allow #{attribute} to be set to \"#{v}\"" do
assert object = klass.find(:first), "Can't find first #{klass}"
object.send("#{attribute}=", v)
object.save
# assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
assert_no_match(/invalid/, object.errors.on(attribute), "Error set on #{attribute} includes \"invalid\" when set to \"#{v}\"")
end
end
end
end
end
# Ensures that the length of the attribute is in the given range
# Requires an existing record
def should_ensure_length_in_range(attribute, range)
klass = self.name.gsub(/Test$/, '').constantize
min_length = range.first
max_length = range.last
# Ensures that the length of the attribute is in the given range
# Requires an existing record
def should_ensure_length_in_range(attribute, range)
klass = self.name.gsub(/Test$/, '').constantize
min_length = range.first
max_length = range.last
min_value = "x" * (min_length - 1)
max_value = "x" * (max_length + 1)
min_value = "x" * (min_length - 1)
max_value = "x" * (max_length + 1)
should "not allow #{attribute} to be less than #{min_length} chars long" do
assert object = klass.find(:first), "Can't find first #{klass}"
object.send("#{attribute}=", min_value)
assert !object.save, "Saved #{klass} with #{attribute} set to \"#{min_value}\""
assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{min_value}\""
assert_match(/short/, object.errors.on(attribute), "Error set on #{attribute} doesn't include \"short\" when set to \"#{min_value}\"")
end
should "not allow #{attribute} to be less than #{min_length} chars long" do
assert object = klass.find(:first), "Can't find first #{klass}"
object.send("#{attribute}=", min_value)
assert !object.save, "Saved #{klass} with #{attribute} set to \"#{min_value}\""
assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{min_value}\""
assert_match(/short/, object.errors.on(attribute), "Error set on #{attribute} doesn't include \"short\" when set to \"#{min_value}\"")
end
should "not allow #{attribute} to be more than #{max_length} chars long" do
assert object = klass.find(:first), "Can't find first #{klass}"
object.send("#{attribute}=", max_value)
assert !object.save, "Saved #{klass} with #{attribute} set to \"#{max_value}\""
assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{max_value}\""
assert_match(/long/, object.errors.on(attribute), "Error set on #{attribute} doesn't include \"long\" when set to \"#{max_value}\"")
end
end
should "not allow #{attribute} to be more than #{max_length} chars long" do
assert object = klass.find(:first), "Can't find first #{klass}"
object.send("#{attribute}=", max_value)
assert !object.save, "Saved #{klass} with #{attribute} set to \"#{max_value}\""
assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{max_value}\""
assert_match(/long/, object.errors.on(attribute), "Error set on #{attribute} doesn't include \"long\" when set to \"#{max_value}\"")
end
end
# Ensure that the attribute is in the range specified
# Requires an existing record
def should_ensure_value_in_range(attribute, range)
klass = self.name.gsub(/Test$/, '').constantize
min = range.first
max = range.last
# Ensure that the attribute is in the range specified
# Requires an existing record
def should_ensure_value_in_range(attribute, range)
klass = self.name.gsub(/Test$/, '').constantize
min = range.first
max = range.last
should "not allow #{attribute} to be less than #{min}" do
v = min - 1
assert object = klass.find(:first), "Can't find first #{klass}"
object.send("#{attribute}=", v)
assert !object.save, "Saved #{klass} with #{attribute} set to \"#{v}\""
assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
end
should "not allow #{attribute} to be less than #{min}" do
v = min - 1
assert object = klass.find(:first), "Can't find first #{klass}"
object.send("#{attribute}=", v)
assert !object.save, "Saved #{klass} with #{attribute} set to \"#{v}\""
assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
end
should "not allow #{attribute} to be more than #{max}" do
v = max + 1
assert object = klass.find(:first), "Can't find first #{klass}"
object.send("#{attribute}=", v)
assert !object.save, "Saved #{klass} with #{attribute} set to \"#{v}\""
assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
end
end
should "not allow #{attribute} to be more than #{max}" do
v = max + 1
assert object = klass.find(:first), "Can't find first #{klass}"
object.send("#{attribute}=", v)
assert !object.save, "Saved #{klass} with #{attribute} set to \"#{v}\""
assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
end
end
# Ensure that the attribute is numeric
# Requires an existing record
def should_only_allow_numeric_values_for(*attributes)
klass = self.name.gsub(/Test$/, '').constantize
attributes.each do |attribute|
attribute = attribute.to_sym
should "only allow numeric values for #{attribute}" do
assert object = klass.find(:first), "Can't find first #{klass}"
object.send(:"#{attribute}=", "abcd")
assert !object.valid?, "Instance is still valid"
assert object.errors.on(attribute), "No errors found"
assert object.errors.on(attribute).to_a.include?('is not a number'), "Error message doesn't match"
# Ensure that the attribute is numeric
# Requires an existing record
def should_only_allow_numeric_values_for(*attributes)
klass = self.name.gsub(/Test$/, '').constantize
attributes.each do |attribute|
attribute = attribute.to_sym
should "only allow numeric values for #{attribute}" do
assert object = klass.find(:first), "Can't find first #{klass}"
object.send(:"#{attribute}=", "abcd")
assert !object.valid?, "Instance is still valid"
assert object.errors.on(attribute), "No errors found"
assert object.errors.on(attribute).to_a.include?('is not a number'), "Error message doesn't match"
end
end
end
end
end
# Ensures that the has_many relationship exists.
# The last parameter may be a hash of options. Currently, the only supported option
# is :through
def should_have_many(*associations)
opts = associations.last.is_a?(Hash) ? associations.pop : {}
klass = self.name.gsub(/Test$/, '').constantize
associations.each do |association|
should "have many #{association}#{" through #{opts[:through]}" if opts[:through]}" do
reflection = klass.reflect_on_association(association)
assert reflection
assert_equal :has_many, reflection.macro
assert_equal(opts[:through], reflection.options[:through]) if opts[:through]
# Ensures that the has_many relationship exists.
# The last parameter may be a hash of options. Currently, the only supported option
# is :through
def should_have_many(*associations)
opts = associations.last.is_a?(Hash) ? associations.pop : {}
klass = self.name.gsub(/Test$/, '').constantize
associations.each do |association|
should "have many #{association}#{" through #{opts[:through]}" if opts[:through]}" do
reflection = klass.reflect_on_association(association)
assert reflection
assert_equal :has_many, reflection.macro
assert_equal(opts[:through], reflection.options[:through]) if opts[:through]
end
end
end
end
end
# Ensures that the has_and_belongs_to_many relationship exists.
def should_have_and_belong_to_many(*associations)
klass = self.name.gsub(/Test$/, '').constantize
associations.each do |association|
should "should have and belong to many #{association}" do
assert klass.reflect_on_association(association)
assert_equal :has_and_belongs_to_many, klass.reflect_on_association(association).macro
# Ensures that the has_and_belongs_to_many relationship exists.
def should_have_and_belong_to_many(*associations)
klass = self.name.gsub(/Test$/, '').constantize
associations.each do |association|
should "should have and belong to many #{association}" do
assert klass.reflect_on_association(association)
assert_equal :has_and_belongs_to_many, klass.reflect_on_association(association).macro
end
end
end
end
end
# Ensure that the has_one relationship exists.
def should_have_one(*associations)
klass = self.name.gsub(/Test$/, '').constantize
associations.each do |association|
should "have one #{association}" do
assert klass.reflect_on_association(association)
assert_equal :has_one, klass.reflect_on_association(association).macro
# Ensure that the has_one relationship exists.
def should_have_one(*associations)
klass = self.name.gsub(/Test$/, '').constantize
associations.each do |association|
should "have one #{association}" do
assert klass.reflect_on_association(association)
assert_equal :has_one, klass.reflect_on_association(association).macro
end
end
end
end
end
# Ensure that the belongs_to relationship exists.
def should_belong_to(*associations)
klass = self.name.gsub(/Test$/, '').constantize
associations.each do |association|
should "belong_to #{association}" do
assert klass.reflect_on_association(association)
assert_equal :belongs_to, klass.reflect_on_association(association).macro
# Ensure that the belongs_to relationship exists.
def should_belong_to(*associations)
klass = self.name.gsub(/Test$/, '').constantize
associations.each do |association|
should "belong_to #{association}" do
assert klass.reflect_on_association(association)
assert_equal :belongs_to, klass.reflect_on_association(association).macro
end
end
end
end
end

View File

@ -1,11 +1,12 @@
module TBTestHelpers
module TBTestHelpers # :nodoc:
module Should
def Should.included(other)
def Should.included(other) # :nodoc:
@@_context_names = []
@@_setup_blocks = []
@@_teardown_blocks = []
end
# Creates a context block with the given name. The context block can contain setup, should, should_eventually, and teardown blocks.
def context(name, &context_block)
@@_context_names << name
context_block.bind(self).call
@ -14,17 +15,17 @@ module TBTestHelpers
@@_teardown_blocks.pop
end
# Run before every should block in the current context
def setup(&setup_block)
@@_setup_blocks << setup_block
end
# Run after every should block in the current context
def teardown(&teardown_block)
@@_teardown_blocks << teardown_block
end
# Defines a specification. Can be called either inside our outside of a context.
#
#
def should(name, opts = {}, &should_block)
unless @@_context_names.empty?
test_name = "test #{@@_context_names.join(" ")} should #{name}"
@ -48,11 +49,13 @@ module TBTestHelpers
define_method test_name_sym do |*args|
setup_block.bind(self).call if setup_block
should_block.bind(self).call(*args)
ensure
teardown_block.bind(self).call if teardown_block
end
end
end
# Defines a specification that is not yet implemented. Will be displayed as an 'X' when running tests, and failures will not be shown.
def should_eventually(name, &block)
should("eventually #{name}", {:unimplemented => true}, &block)
end

View File

@ -1,48 +1,52 @@
require 'active_record_helpers'
require 'should'
class Test::Unit::TestCase
class << self
include TBTestHelpers::Should
class Test # :nodoc:
class Unit # :nodoc:
class TestCase
class << self
include TBTestHelpers::Should
# Loads all fixture files
def load_all_fixtures
all_fixtures = Dir.glob(File.join(RAILS_ROOT, "test", "fixtures", "*.yml")).collect do |f|
File.basename(f, '.yml').to_sym
end
fixtures *all_fixtures
end
# Loads all fixture files
def load_all_fixtures
all_fixtures = Dir.glob(File.join(RAILS_ROOT, "test", "fixtures", "*.yml")).collect do |f|
File.basename(f, '.yml').to_sym
end
fixtures *all_fixtures
end
# Ensures that the number of items in the collection changes
def assert_difference(object, method, difference, reload = false)
initial_value = object.send(method)
yield
reload and object.send(:reload)
assert_equal initial_value + difference, object.send(method), "#{object}##{method} after block"
end
# Ensures that object.method does not change
def assert_no_difference(object, method, reload = false, &block)
assert_difference(object, method, 0, reload, &block)
end
# Logs a message, tagged with TESTING: and the name of the calling method.
def report!(msg = "")
@controller.logger.info("TESTING: #{caller.first}: #{msg}")
end
# asserts that two arrays contain the same elements, the same number of times. Essentially ==, but unordered.
def assert_same_elements(a1, a2)
[:select, :inject, :size].each do |m|
[a1, a2].each {|a| assert_respond_to(a, m, "Are you sure that #{a} is an array?") }
end
assert a1h = a1.inject({}){|h,e| h[e] = a1.select{|i| i == e}.size; h}
assert a2h = a2.inject({}){|h,e| h[e] = a2.select{|i| i == e}.size; h}
end
# Ensures that the number of items in the collection changes
def assert_difference(object, method, difference, reload = false)
initial_value = object.send(method)
yield
reload and object.send(:reload)
assert_equal initial_value + difference, object.send(method), "#{object}##{method} after block"
end
# Ensures that object.method does not change
def assert_no_difference(object, method, reload = false, &block)
assert_difference(object, method, 0, reload, &block)
end
def report!(msg = "")
@controller.logger.info("TESTING: #{caller.first}: #{msg}")
end
# asserts that two arrays contain the same elements, the same number of times. Essentially ==, but unordered.
def assert_same_elements(a1, a2)
[:select, :inject, :size].each do |m|
[a1, a2].each {|a| assert_respond_to(a, m, "Are you sure that #{a} is an array?") }
assert_equal(a1, a2)
end
end
assert a1h = a1.inject({}){|h,e| h[e] = a1.select{|i| i == e}.size; h}
assert a2h = a2.inject({}){|h,e| h[e] = a2.select{|i| i == e}.size; h}
assert_equal(a1, a2)
end
end

View File

@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), 'test_helper')
class ContextTest < Test::Unit::TestCase
class ContextTest < Test::Unit::TestCase # :nodoc:
context "context with setup block" do
setup do