Module: Test::Unit::Assertions

Defined in:
lib/test/zentest_assertions.rb,
lib/test/rails/test_case.rb

Overview

Extra assertions for Test::Unit

Instance Method Summary collapse

Instance Method Details

#assert_callback(model_class, callback, method_name, message = nil) ⇒ Object

TODO: should this go in this file? Asserts that model indeed has a given callback

assert_callback(Model, :before_save, :something)



19
20
21
22
23
# File 'lib/test/rails/test_case.rb', line 19

def assert_callback(model_class, callback, method_name, message=nil)
  vars = model_class.instance_variable_get(:@inheritable_attributes)
  assert vars.has_key?(callback), message
  assert_include vars[callback], method_name, message
end

#assert_empty(obj) ⇒ Object

Asserts that obj responds to #empty? and #empty? returns true.



25
26
27
28
# File 'lib/test/zentest_assertions.rb', line 25

def assert_empty(obj)
  assert_respond_to obj, :empty?
  assert_block "#{obj.inspect} expected to be empty." do obj.empty? end
end

#assert_in_epsilon(a, b, epsilon, message = nil) ⇒ Object

Like assert_in_delta but better dealing with errors proportional to the sizes of a and b.



34
35
36
37
38
39
40
41
# File 'lib/test/zentest_assertions.rb', line 34

def assert_in_epsilon(a, b, epsilon, message = nil)
  return assert(true) if a == b # count assertion

  error = ((a - b).to_f / ((b.abs > a.abs) ? b : a)).abs
  message ||= "#{a} expected to be within #{epsilon * 100}% of #{b}, was #{error}"

  assert_block message do error <= epsilon end
end