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_changed.rb

Instance Method Summary collapse

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