Class: Test::Unit::TestCase

Inherits:
Object show all
Defined in:
lib/qualitysmith_extensions/test/assert_user_error.rb,
lib/qualitysmith_extensions/test/assert_exception.rb,
lib/qualitysmith_extensions/test/assert_includes.rb,
lib/qualitysmith_extensions/test/assert_anything.rb,
lib/qualitysmith_extensions/test/assert_changed.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Lets you make an assertion out of any method, without having to write a new assert_ method for it!

So as long as the whatever method’s return value can be interpreted as a boolean value, you can simply call assert_whatever a, b, which will be equivalent to calling assert a.whatever(b)

Follow this basic pattern:

assert_{method} {receiver}, {args}
assert_not_{method} {receiver}, {args}
assert_{method}_is      {receiver}, {args}, {expected_return_value}
assert_{method}_returns {receiver}, {args}, {expected_return_value}

Examples:

assert_include? [1, 2, 3], 2
assert_not_include? [1, 2, 3], 4
assert_class_is 'foo', String


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/qualitysmith_extensions/test/assert_anything.rb', line 28

def method_missing(name, *args)
  # to do: 
  #   options = args.pop if args.last.is_a?(Hash)
  #   message = options[:message]
  if name.to_s =~ /^assert_(.*)/
    receiver = args.shift
    negated = false
    message_to_pass = $1

    if name.to_s =~ /^assert_(.*)_is/
      message_to_pass = $1
      expected = args.pop
      if name.to_s =~ /^assert_(.*)_is_not/
        message_to_pass = $1
        negated = true
      end

      result = receiver.send(message_to_pass, *args)
      if negated
        assert_not_equal expected, result
      else
        assert_equal expected, result
      end
    else
      if name.to_s =~ /^assert_not_(.*)|assert_(.*)_is_not/
        message_to_pass = $1
        negated = true
      end

      result = receiver.send(message_to_pass, *args)
      result = !result if negated
      assert result
    end

  else
    super
  end
end

Instance Method Details

#assert_changed(variable, options = {}, &block) ⇒ Object

Asserts that the block that is passed in causes the value of the specified variable (variable) to change. variable should be a Proc that, when evaluated, returns the current value of the variable.

Options:

  • If the optional :from option is supplied, it also asserts that it had that initial value.

  • If the optional :to option is supplied, it also asserts that it changed to that value.

So instead of doing this:

assert_equal 1, Model.count
do_something_that_should_cause_count_to_increase
assert_equal 2, Model.count

we can do this:

assert_changed(lambda {ErrorType.count}, :from => 1, :to => 2) do
  do_something_that_should_cause_count_to_increase
end

Or, if we don’t care what it’s changing from as long as it increases in value by 1, we can write this:

assert_changed(c = lambda {ErrorType.count}, :to => c.call+1) do
  do_something_that_should_cause_count_to_increase
end

instead of this:

before = Model.count
do_something_that_should_cause_count_to_increase
assert_equal before + 1, Model.count


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/qualitysmith_extensions/test/assert_changed.rb', line 35

def assert_changed(variable, options = {}, &block)
  expected_from = options.delete(:from) || variable.call

  assert_equal expected_from, variable.call

  failure_message = build_message(failure_message, "The variable was expected to change from <?> to <?> but it didn't", variable.call, options.delete(:to) || "something else")
  assert_block(failure_message) do
    before = variable.call
    yield
    expected_to = options.delete(:to) || variable.call
    before != variable.call and variable.call == expected_to
  end
end

#assert_exception(expected_class, additional_expectations = nil, &block) ⇒ Object

Used when you want to make assertions about an assertion that you expect to be raised. (With the built-in assert_raise() you can only assert that a particular class of exception is raised, not any specifics about it.

Before:

exception = nil
assert_raises(ArgumentError) { SomeCommand.execute("foo '''") }
begin
  SomeCommand.execute("foo -m '''")
rescue Exception => _exception
  exception = _exception
end
assert_equal "Unmatched single quote: '", exception.message

After:

assert_exception(ArgumentError, lambda { |exception|
  assert_match /Unmatched single quote/, exception.message
}) do
  SomeCommand.execute("foo -m 'stuff''")
end


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/qualitysmith_extensions/test/assert_exception.rb', line 33

def assert_exception(expected_class, additional_expectations = nil, &block)
  exception = nil
  assert_raise(expected_class) do
    begin
      yield
    rescue Exception => _exception
      exception = _exception
      raise
    end
  end
  additional_expectations.call(exception) if additional_expectations
end

#assert_includes(container, expected_contents, failure_message = nil) ⇒ Object Also known as: assert_contains



10
11
12
13
14
15
# File 'lib/qualitysmith_extensions/test/assert_includes.rb', line 10

def assert_includes(container, expected_contents, failure_message = nil)
  failure_message = build_message(failure_message, "Container <?> was expected to contain <?> but it didn't", container, expected_contents)
  assert_block(failure_message) do
    container.include?(expected_contents)
  end
end

#assert_user_error(error_message) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/qualitysmith_extensions/test/assert_user_error.rb', line 11

def assert_user_error(error_message)
  assert_tag({
    :attributes => { :id => "errorExplanation" },
    :descendant => { 
      :content => error_message
    }
  })
end