The assert_difference
test method in Rails is nice, but I found myself wanting a bit more flexibility. Like passing arguments to the method, not having to specify the exact change and checking for changes to things apart from numbers. So I wrote this. It gives you assert_change
and assert_no_change
which really just run the same thing twice. You could use a lambda sort of thing instead, which might be clearer. And it probably needs some kind of way of attaching a message. But I am mostly happy with it.
module Test::Unit::AssertChangeHelper
def assert_change(object, *args, &block)
old, new = before_and_after(object, *args, &block)
assert_not_equal old, new
end
def assert_no_change(object, *args, &block)
old, new = before_and_after(object, *args, &block)
assert_equal old, new
end
protected
def before_and_after(object, *args, &block)
old = object.send(*args)
block.call
new = object.send(*args)
return old, new
end
end
You would use it like this:
assert_change(Visit, :find, :all, :conditions => ['is_exception = 1']) do
Visit.create(options)
end
Comments
No comments yet.
Leave a comment