Module: Hardmock
- Included in:
- Test::Unit::TestCase
- Defined in:
- lib/hardmock/method_cleanout.rb,
lib/hardmock.rb,
lib/hardmock/mock.rb,
lib/hardmock/utils.rb,
lib/hardmock/errors.rb,
lib/hardmock/trapper.rb,
lib/hardmock/expector.rb,
lib/hardmock/stubbing.rb,
lib/hardmock/expectation.rb,
lib/hardmock/mock_control.rb,
lib/hardmock/expectation_builder.rb
Overview
:nodoc:
Defined Under Namespace
Modules: MethodCleanout, Stubbing, Utils Classes: DeprecationError, Expectation, ExpectationBuilder, ExpectationError, Expector, Mock, MockControl, ReplacedMethod, StubbedMethod, StubbingError, Trapper, VerifyError
Class Method Summary collapse
- .all_replaced_methods ⇒ Object
- .has_replaced_method?(obj, method_name) ⇒ Boolean
- .restore_all_replaced_methods ⇒ Object
- .track_replaced_method(replaced_method) ⇒ Object
Instance Method Summary collapse
-
#clear_expectations ⇒ Object
Purge the main MockControl of all expectations, restore all concrete stubbed/mocked methods.
-
#create_mocks(*mock_names) ⇒ Object
(also: #create_mock)
Create one or more new Mock instances in your test suite.
- #prepare_hardmock_control ⇒ Object
- #reset_stubs ⇒ Object
-
#verify_mocks(force = true) ⇒ Object
(also: #verify_hardmocks)
Ensures that all expectations have been met.
Class Method Details
.all_replaced_methods ⇒ Object
182 183 184 |
# File 'lib/hardmock/stubbing.rb', line 182 def all_replaced_methods $all_replaced_methods ||= [] end |
.has_replaced_method?(obj, method_name) ⇒ Boolean
186 187 188 189 190 191 |
# File 'lib/hardmock/stubbing.rb', line 186 def has_replaced_method?(obj, method_name) hits = all_replaced_methods.select do |replaced| (replaced.target.object_id == obj.object_id) and (replaced.method_name.to_s == method_name.to_s) end return !hits.empty? end |
.restore_all_replaced_methods ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/hardmock/stubbing.rb', line 193 def restore_all_replaced_methods all_replaced_methods.each do |replaced| unless replaced.target._is_mock? backed_up = "_hardmock_original_#{replaced.method_name}" if replaced.target.methods.include?(backed_up) replaced.target. do alias_method replaced.method_name.to_sym, backed_up.to_sym end end replaced.target._clear_mock end end all_replaced_methods.clear end |
.track_replaced_method(replaced_method) ⇒ Object
178 179 180 |
# File 'lib/hardmock/stubbing.rb', line 178 def track_replaced_method(replaced_method) all_replaced_methods << replaced_method end |
Instance Method Details
#clear_expectations ⇒ Object
Purge the main MockControl of all expectations, restore all concrete stubbed/mocked methods
74 75 76 77 78 |
# File 'lib/hardmock.rb', line 74 def clear_expectations @main_mock_control.clear_expectations if @main_mock_control reset_stubs $main_mock_control = nil end |
#create_mocks(*mock_names) ⇒ Object Also known as: create_mock
Create one or more new Mock instances in your test suite. Once created, the Mocks are accessible as instance variables in your test. Newly built Mocks are added to the full set of Mocks for this test, which will be verified when you call verify_mocks.
create_mocks :donkey, :cat # Your test now has @donkey and @cat
create_mock :dog # Test now has @donkey, @cat and @dog
The first call returned a hash { :donkey => @donkey, :cat => @cat } and the second call returned { :dog => @dog }
For more info on how to use your mocks, see Mock and Expectation
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/hardmock.rb', line 27 def create_mocks(*mock_names) prepare_hardmock_control unless @main_mock_control mocks = {} mock_names.each do |mock_name| raise ArgumentError, "'nil' is not a valid name for a mock" if mock_name.nil? mock_name = mock_name.to_s mock_object = Mock.new(mock_name, @main_mock_control) mocks[mock_name.to_sym] = mock_object self.instance_variable_set "@#{mock_name}", mock_object end @all_mocks ||= {} @all_mocks.merge! mocks return mocks.clone end |
#prepare_hardmock_control ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/hardmock.rb', line 44 def prepare_hardmock_control if @main_mock_control.nil? @main_mock_control = MockControl.new $main_mock_control = @main_mock_control else raise "@main_mock_control is already setup for this test!" end end |
#reset_stubs ⇒ Object
80 81 82 |
# File 'lib/hardmock.rb', line 80 def reset_stubs Hardmock.restore_all_replaced_methods end |
#verify_mocks(force = true) ⇒ Object Also known as: verify_hardmocks
Ensures that all expectations have been met. If not, VerifyException is raised.
You normally won’t need to call this yourself. Within Test::Unit::TestCase, this will be done automatically at teardown time.
-
force
– iffalse
, and a VerifyError or ExpectationError has already occurred, this method will not raise. This is to help you suppress repeated errors when if you’re calling #verify_mocks in the teardown method of your test suite. BE WARNED - only use this if you’re sure you aren’t obscuring useful information. Eg, if your code handles exceptions internally, and an ExpectationError gets gobbled up by yourrescue
block, the cause of failure for your test may be hidden from you. For this reason, #verify_mocks defaults to force=true as of Hardmock 1.0.1
61 62 63 64 65 66 67 68 69 |
# File 'lib/hardmock.rb', line 61 def verify_mocks(force=true) return unless @main_mock_control return if @main_mock_control.disappointed? and !force @main_mock_control.verify ensure @main_mock_control.clear_expectations if @main_mock_control $main_mock_control = nil reset_stubs end |