testing scope option

git-svn-id: https://svn.thoughtbot.com/plugins/tb_test_helpers/trunk@69 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
This commit is contained in:
tsaleh 2007-04-04 17:39:56 +00:00
parent b9d0a4dcc7
commit 213c328e86
4 changed files with 51 additions and 6 deletions

View File

@ -1 +1,3 @@
require 'rubygems'
require 'active_support'
require 'tb_test_helpers'

View File

@ -5,13 +5,21 @@ module Test # :nodoc:
# 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)
opts[:message] ||= /blank/
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"
case opts[:message]
when Regex:
assert(object.errors.on(attribute).to_a.detect {|e| e =~ opts[:message]},
"#{opts[:message]} not found in #{object.errors.on(attribute).to_a.inspect}")
when String:
assert(object.errors.on(attribute).to_a.include?(opts[:message]),
"#{opts[:message]} not found in #{object.errors.on(attribute).to_a.inspect}")
end
end
end
end
@ -19,16 +27,51 @@ module Test # :nodoc:
# 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)
opts = attributes.last.is_a?(Hash) ? attributes.pop : {}
opts[:message] ||= /taken/
scope = opts[:scoped_to]
klass = self.name.gsub(/Test$/, '').constantize
attributes.each do |attribute|
attribute = attribute.to_sym
should "require unique value for #{attribute}" do
should "require unique value for #{attribute}#{" scoped to #{scope}" if scope}" do
assert existing = klass.find(:first), "Can't find first #{klass}"
object = klass.new
object.send(:"#{attribute}=", existing.send(attribute))
if scope
assert_respond_to object, :"#{scope}="
object.send(:"#{scope}=", existing.send(scope))
end
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"
case opts[:message]
when Regex:
assert(object.errors.on(attribute).to_a.detect {|e| e =~ opts[:message]},
"#{opts[:message]} not found in #{object.errors.on(attribute).to_a.inspect}")
when String:
assert(object.errors.on(attribute).to_a.include?(opts[:message]),
"#{opts[:message]} not found in #{object.errors.on(attribute).to_a.inspect}")
end
if scope
# Now test that the object is valid when changing the scoped attribute
object.send(:"#{scoped}=", existing.send(scope).nil? ? 1 : existing.send(scoped_attr).next)
object.errors.clear
object.valid?
case opts[:message]
when Regex:
assert(! object.errors.on(attribute).to_a.detect {|e| e =~ opts[:message]},
"#{opts[:message]} not found in #{object.errors.on(attribute).to_a.inspect}")
when String:
assert(! object.errors.on(attribute).to_a.include?(opts[:message]),
"#{opts[:message]} not found in #{object.errors.on(attribute).to_a.inspect}")
end
end
end
end
end

View File

@ -33,8 +33,9 @@ module TBTestHelpers # :nodoc:
# Defines a specification. Can be called either inside our outside of a context.
def should(name, opts = {}, &should_block)
test_name = ["test", @@context_names, "should", "#{name}"].flatten.join(' ').to_sym
raise ArgumentError, "'#{test_name}' is already defined" and return if self.instance_methods.include? test_name
name_defined = eval("self.instance_methods.include?('#{test_name.to_s.gsub(/['"]/, '\$1')}')", should_block.binding)
raise ArgumentError, "'#{test_name}' is already defined" and return if name_defined
setup_blocks = @@setup_blocks.dup
teardown_blocks = @@teardown_blocks.dup

View File

@ -12,7 +12,6 @@ class ContextTest < Test::Unit::TestCase # :nodoc:
end
should "have name set right" do
raise RuntimeError, "Whops!"
assert_match(/^test context with setup block/, self.to_s)
end