Added scope_to to AR helper

git-svn-id: https://svn.thoughtbot.com/plugins/tb_test_helpers/trunk@70 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
This commit is contained in:
tsaleh 2007-04-04 18:06:24 +00:00
parent 213c328e86
commit 6a0e0f5f29
2 changed files with 12 additions and 11 deletions

View File

@ -5,6 +5,7 @@ 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 = attributes.last.is_a?(Hash) ? attributes.pop : {}
opts[:message] ||= /blank/
klass = self.name.gsub(/Test$/, '').constantize
attributes.each do |attribute|
@ -13,7 +14,7 @@ module Test # :nodoc:
assert !object.valid?, "Instance is still valid"
assert object.errors.on(attribute), "No errors found"
case opts[:message]
when Regex:
when Regexp:
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:
@ -48,7 +49,7 @@ module Test # :nodoc:
assert object.errors.on(attribute), "No errors found"
case opts[:message]
when Regex:
when Regexp:
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:
@ -58,17 +59,17 @@ module Test # :nodoc:
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.send(:"#{scope}=", existing.send(scope).nil? ? 1 : existing.send(scope).next)
object.errors.clear
object.valid?
case opts[:message]
when Regex:
when Regexp:
assert(!object.errors.on(attribute).to_a.detect {|e| e =~ opts[:message]},
"#{opts[:message]} not found in #{object.errors.on(attribute).to_a.inspect}")
"#{opts[:message].inspect} found in #{object.errors.on(attribute).to_a.inspect} after :#{scope} set to #{object.send(scope.to_sym)}")
when String:
assert(!object.errors.on(attribute).to_a.include?(opts[:message]),
"#{opts[:message]} not found in #{object.errors.on(attribute).to_a.inspect}")
"#{opts[:message].inspect} found in #{object.errors.on(attribute).to_a.inspect} after :#{scope} set to #{object.send(scope.to_sym)}")
end
end

View File

@ -45,7 +45,7 @@ module Test # :nodoc:
# asserts that two arrays contain the same elements, the same number of times. Essentially ==, but unordered.
def assert_same_elements(a1, a2, msg = nil)
[:select, :inject, :size].each do |m|
[a1, a2].each {|a| assert_respond_to(a, m, "Are you sure that #{a} is an array?") }
[a1, a2].each {|a| assert_respond_to(a, m, "Are you sure that #{a.inspect} is an array? It doesn't respond to #{m}.") }
end
assert a1h = a1.inject({}) { |h,e| h[e] = a1.select { |i| i == e }.size; h }