Class: Assert::Context
- Inherits:
-
Object
- Object
- Assert::Context
- Extended by:
- SetupDSL, SubjectDSL, SuiteDSL, TestDSL
- Includes:
- Assertions, Macros::Methods
- Defined in:
- lib/assert.rb,
lib/assert/context.rb,
lib/assert/context/test_dsl.rb,
lib/assert/context/setup_dsl.rb,
lib/assert/context/suite_dsl.rb,
lib/assert/context/subject_dsl.rb
Overview
unstub all stubs automatically (see stub.rb)
Defined Under Namespace
Modules: SetupDSL, SubjectDSL, SuiteDSL, TestDSL
Constant Summary
Constants included from Assertions
Assertions::IGNORED_ASSERTION_HELPERS
Class Method Summary collapse
-
.method_added(method_name) ⇒ Object
if a test method is added to a context manually (not using a context helper): capture any context info, build a test obj, and add it to the suite.
Instance Method Summary collapse
-
#assert(assertion, desc = nil) ⇒ Object
check if the assertion is a truthy value, if so create a new pass result, otherwise create a new fail result with the desc and what failed msg.
-
#assert_not(assertion, fail_desc = nil) ⇒ Object
(also: #refute)
the opposite of assert, check if the assertion is a false value, if so create a new pass result, otherwise create a new fail result with the desc and it’s what failed msg.
-
#fail(message = nil) ⇒ Object
(also: #flunk)
adds a Fail result to the end of the test’s results break test execution if assert is configured to halt on failures.
-
#ignore(ignore_msg = nil) ⇒ Object
adds an Ignore result to the end of the test’s results does not break test execution.
-
#initialize(running_test, config, result_callback) ⇒ Context
constructor
A new instance of Context.
- #inspect ⇒ Object
-
#pass(pass_msg = nil) ⇒ Object
adds a Pass result to the end of the test’s results does not break test execution.
-
#skip(skip_msg = nil, called_from = nil) ⇒ Object
adds a Skip result to the end of the test’s results and breaks test execution.
- #subject ⇒ Object
-
#with_backtrace(bt, &block) ⇒ Object
alter the backtraces of fail results generated in the given block.
Methods included from SetupDSL
around, arounds, run_arounds, run_setups, run_teardowns, setup, setup_once, setups, teardown, teardown_once, teardowns
Methods included from SubjectDSL
Methods included from SuiteDSL
Methods included from TestDSL
should, should_eventually, test, test_eventually
Methods included from Macros::Methods
Methods included from Assertions
#assert_block, #assert_empty, #assert_equal, #assert_false, #assert_file_exists, #assert_includes, #assert_instance_of, #assert_kind_of, #assert_match, #assert_nil, #assert_not_block, #assert_not_empty, #assert_not_equal, #assert_not_false, #assert_not_file_exists, #assert_not_includes, #assert_not_instance_of, #assert_not_kind_of, #assert_not_match, #assert_not_nil, #assert_not_respond_to, #assert_not_same, #assert_not_true, #assert_nothing_raised, #assert_raises, #assert_respond_to, #assert_same, #assert_true, #method_missing
Constructor Details
#initialize(running_test, config, result_callback) ⇒ Context
Returns a new instance of Context.
49 50 51 52 53 |
# File 'lib/assert/context.rb', line 49 def initialize(running_test, config, result_callback) @__running_test__ = running_test @__assert_config__ = config @__result_callback__ = result_callback end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Assert::Assertions
Class Method Details
.method_added(method_name) ⇒ Object
if a test method is added to a context manually (not using a context helper): capture any context info, build a test obj, and add it to the suite
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/assert/context.rb', line 30 def self.method_added(method_name) if method_name.to_s =~ Suite::TEST_METHOD_REGEX klass_method_name = "#{self}##{method_name}" if self.suite.test_methods.include?(klass_method_name) puts "WARNING: redefining '#{klass_method_name}'" puts " from: #{caller.first}" else self.suite.test_methods << klass_method_name end self.suite.tests << Test.for_method( method_name.to_s, Suite::ContextInfo.new(self, nil, caller.first), self.suite.config ) end end |
Instance Method Details
#assert(assertion, desc = nil) ⇒ Object
check if the assertion is a truthy value, if so create a new pass result, otherwise create a new fail result with the desc and what failed msg. all other assertion helpers use this one in the end
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/assert/context.rb', line 58 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((desc, what)) end end |
#assert_not(assertion, fail_desc = nil) ⇒ Object Also known as: refute
the opposite of assert, check if the assertion is a false value, if so create a new pass result, otherwise create a new fail result with the desc and it’s what failed msg
73 74 75 76 77 |
# File 'lib/assert/context.rb', line 73 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 |
#fail(message = nil) ⇒ Object Also known as: flunk
adds a Fail result to the end of the test’s results break test execution if assert is configured to halt on failures
98 99 100 101 102 103 104 105 106 |
# File 'lib/assert/context.rb', line 98 def fail( = nil) if halt_on_fail? raise Result::TestFailure, || '' else capture_result do |test, backtrace| Assert::Result::Fail.for_test(test, || '', backtrace) end end end |
#ignore(ignore_msg = nil) ⇒ Object
adds an Ignore result to the end of the test’s results does not break test execution
90 91 92 93 94 |
# File 'lib/assert/context.rb', line 90 def ignore(ignore_msg = nil) capture_result do |test, backtrace| Assert::Result::Ignore.for_test(test, ignore_msg, backtrace) end end |
#inspect ⇒ Object
136 137 138 |
# File 'lib/assert/context.rb', line 136 def inspect "#<#{self.class}>" end |
#pass(pass_msg = nil) ⇒ Object
adds a Pass result to the end of the test’s results does not break test execution
82 83 84 85 86 |
# File 'lib/assert/context.rb', line 82 def pass(pass_msg = nil) capture_result do |test, backtrace| Assert::Result::Pass.for_test(test, pass_msg, backtrace) end end |
#skip(skip_msg = nil, called_from = nil) ⇒ Object
adds a Skip result to the end of the test’s results and breaks test execution
110 111 112 113 114 |
# File 'lib/assert/context.rb', line 110 def skip(skip_msg = nil, called_from = nil) err = Result::TestSkipped.new(skip_msg || '') err.set_backtrace([called_from]) if called_from raise(err) end |
#subject ⇒ Object
130 131 132 133 134 |
# File 'lib/assert/context.rb', line 130 def subject if subj = self.class.subject instance_eval(&subj) end end |
#with_backtrace(bt, &block) ⇒ Object
alter the backtraces of fail results generated in the given block
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/assert/context.rb', line 117 def with_backtrace(bt, &block) bt ||= [] current_results.count.tap do |count| begin instance_eval(&block) rescue Result::TestSkipped, Result::TestFailure => e e.set_backtrace(bt); raise(e) ensure current_results[count..-1].each{ |r| r.set_backtrace(bt) } end end end |