Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/quarry/stub/mock.rb,
lib/quarry/assert.rb,
lib/quarry/stub/stub.rb
Overview
class Stub
Instance Method Summary collapse
-
#assert ⇒ Object
(also: #should)
Assert a operational relationship.
-
#assert! ⇒ Object
(also: #refute)
Assert not an operational relationship.
-
#assert_raises(exception, &block) ⇒ Object
(also: #should_raise)
Provides a way to assert that a procedure raises an exception.
-
#mock(mock_module = nil) ⇒ Object
Create mock object.
-
#refute_raises(exception, &block) ⇒ Object
(also: #assert_raises!, #should_not_raise)
Provides a way to assert that a procedure does not raise an exception.
-
#remove(stub_module = nil) ⇒ Object
We can’t remove the module per-say.
-
#stub(stub_module = nil) ⇒ Object
Create a new stub.
Instance Method Details
#assert ⇒ Object Also known as: should
Assert a operational relationship.
4.assert == 3 #=> Assertion Error
119 120 121 |
# File 'lib/quarry/assert.rb', line 119 def assert return Assertion::True.new(self) end |
#assert! ⇒ Object Also known as: refute
Assert not an operational relationship. Read it as “assert not”.
4.assert! == 4 #=> Assertion Error
AUHTOR’S NOTE: This method would not be necessary if Ruby would allow != to be define as a method, or at least ! as a unary method.
142 143 144 |
# File 'lib/quarry/assert.rb', line 142 def assert! return Assertion::False.new(self) end |
#assert_raises(exception, &block) ⇒ Object Also known as: should_raise
Provides a way to assert that a procedure raises an exception.
assert_raises(StandardError){ raise }
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/quarry/assert.rb', line 161 def assert_raises(exception, &block) Assertion::True.new( lambda do |*a| begin block.call(*a) false rescue exception true end end ) end |
#mock(mock_module = nil) ⇒ Object
Create mock object.
63 64 65 66 67 68 69 70 71 |
# File 'lib/quarry/stub/mock.rb', line 63 def mock(mock_module=nil) if mock_module Mock::Delegator.new(self, mock_module) else @_mock ||= Mock.new extend(@_mock) @_mock end end |
#refute_raises(exception, &block) ⇒ Object Also known as: assert_raises!, should_not_raise
Provides a way to assert that a procedure does not raise an exception.
refute_raises(StandardError){ raise }
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/quarry/assert.rb', line 181 def refute_raises(exception, &block) Assertion::True.new( begin block.call(*a) true rescue exception false end ) end |
#remove(stub_module = nil) ⇒ Object
We can’t remove the module per-say. So we have to just neuter it. This is a very weak solution, but it will suffice for the moment. – TODO: Use Carats for #unmix. ++
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/quarry/stub/mock.rb', line 79 def remove(mock_module=nil) mock_module ||= @_mock obj = self mod = Module.new mock_module.__table__.each do |interface, result| meth = interface[0] mod.module_eval do define_method(meth, &obj.class.instance_method(meth).bind(obj)) end end extend(mod) end |
#stub(stub_module = nil) ⇒ Object
Create a new stub.
61 62 63 64 65 66 67 68 69 |
# File 'lib/quarry/stub/stub.rb', line 61 def stub(stub_module=nil) if stub_module Stub::Delegator.new(self, stub_module) else @_stub ||= Stub.new extend(@_stub) @_stub end end |