class Assert::Context

unstub all stubs automatically (see stub.rb)

A Context is a scope for tests to run in. Contexts have setup and teardown blocks, subjects, and descriptions. Tests are run in the scope of a Context instance. Therefore, a Context should have minimal base logic/methods/instance_vars. The instance should remain pure to not pollute test scopes.

Attributes

__assert_config__[R]

Public Class Methods

new(running_test, config, result_callback) click to toggle source
# File lib/assert/context.rb, line 34
def initialize(running_test, config, result_callback)
  @__assert_running_test__ = running_test
  @__assert_config__       = config
  @__assert_with_bt__      = []
  @__assert_pending__      = 0

  @__assert_result_callback__ =
    proc do |result|
      unless @__assert_with_bt__.empty?
        result.set_with_bt(@__assert_with_bt__.dup)
      end
      result_callback.call(result)
      result
    end
end

Public Instance Methods

assert(assertion, desc = nil) { || ... } click to toggle source

Check if the result is true. If so, create a new pass result, Otherwise create a new fail result with the desc and fail msg.

# File lib/assert/context.rb, line 52
def assert(assertion, desc = nil)
  if assertion
    pass
  else
    what =
      if block_given?
        yield
      else
        "Failed assert: assertion was "\
        "`#{Assert::U.show(assertion, __assert_config__)}`."
      end
    fail(fail_message(desc, what))
  end
end
assert_not(assertion, fail_desc = nil) click to toggle source

The opposite of assert. Check if the result is false. If so, create a new pass result. Otherwise create a new fail result with the desc and fail msg.

# File lib/assert/context.rb, line 70
def assert_not(assertion, fail_desc = nil)
  assert(!assertion, fail_desc) do
    "Failed assert_not: assertion was "\
    "`#{Assert::U.show(assertion, __assert_config__)}`."
  end
end
Also aliased as: refute
assert_that( actual_value = Assert::ActualValue.not_given, &actual_value_block) click to toggle source
# File lib/assert/context.rb, line 78
def assert_that(
      actual_value = Assert::ActualValue.not_given,
      &actual_value_block)
  Assert::ActualValue.new(actual_value, context: self, &actual_value_block)
end
fail(message = nil) click to toggle source

adds a Fail result to the end of the test’s results break test execution if assert is configured to halt on failures

# File lib/assert/context.rb, line 105
def fail(message = nil)
  if @__assert_pending__ == 0
    if halt_on_fail?
      raise Result::TestFailure, message || ""
    else
      capture_result(Assert::Result::Fail, message || "")
    end
  elsif halt_on_fail?
    raise Result::TestSkipped, "Pending fail: #{message || ""}"
  else
    capture_result(Assert::Result::Skip, "Pending fail: #{message || ""}")
  end
end
Also aliased as: flunk
flunk(message = nil)
Alias for: fail
ignore(ignore_msg = nil) click to toggle source

adds an Ignore result to the end of the test’s results does not break test execution

# File lib/assert/context.rb, line 99
def ignore(ignore_msg = nil)
  capture_result(Assert::Result::Ignore, ignore_msg)
end
inspect() click to toggle source
# File lib/assert/context.rb, line 161
def inspect
  "#<#{self.class}>"
end
pass(pass_msg = nil) click to toggle source

adds a Pass result to the end of the test’s results does not break test execution

# File lib/assert/context.rb, line 86
def pass(pass_msg = nil)
  if @__assert_pending__ == 0
    capture_result(Assert::Result::Pass, pass_msg)
  else
    capture_result(
      Assert::Result::Fail,
      "Pending pass (make it not pending)",
    )
  end
end
pending(&block) click to toggle source

runs block and any fails are skips and any passes are fails

# File lib/assert/context.rb, line 127
def pending(&block)
  begin
    @__assert_pending__ += 1
    instance_eval(&block)
  ensure
    @__assert_pending__ -= 1
  end
end
refute(assertion, fail_desc = nil)
Alias for: assert_not
skip(skip_msg = nil, called_from = nil) click to toggle source

adds a Skip result to the end of the test’s results breaks test execution

# File lib/assert/context.rb, line 122
def skip(skip_msg = nil, called_from = nil)
  raise Result::TestSkipped, (skip_msg || ""), called_from
end
subject() click to toggle source
# File lib/assert/context.rb, line 152
def subject
  unless instance_variable_defined?("@__assert_subject__")
    @__assert_subject__ =
      instance_eval(&self.class.subject) if self.class.subject
  end

  @__assert_subject__
end
with_backtrace(bt, &block) click to toggle source

alter the backtraces of fail/skip results generated in the given block

# File lib/assert/context.rb, line 137
def with_backtrace(bt, &block)
  bt ||= []
  begin
    @__assert_with_bt__.push(bt.first)
    instance_eval(&block)
  rescue Result::TestSkipped, Result::TestFailure => ex
    if ex.assert_with_bt.nil? && !@__assert_with_bt__.empty?
      ex.assert_with_bt = @__assert_with_bt__.dup
    end
    raise(ex)
  ensure
    @__assert_with_bt__.pop
  end
end

Protected Instance Methods

fail_message(fail_desc = nil, what_failed_msg = nil) click to toggle source

Returns a Proc that will output a custom message along with the default fail message.

# File lib/assert/context.rb, line 169
def fail_message(fail_desc = nil, what_failed_msg = nil)
  [fail_desc, what_failed_msg].compact.join("\n")
end

Private Instance Methods

capture_result(result_klass, msg) click to toggle source
# File lib/assert/context.rb, line 179
def capture_result(result_klass, msg)
  @__assert_result_callback__.call(
    result_klass.for_test(@__assert_running_test__, msg, caller_locations),
  )
end
halt_on_fail?() click to toggle source
# File lib/assert/context.rb, line 175
def halt_on_fail?
  __assert_config__.halt_on_fail
end