Properties¶
doublex supports stub and spy properties in a pretty easy way in relation to other frameworks like python-mock.
That requires two constraints:
- It does not support “free” doubles. ie: you must give a collaborator in the constructor.
- collaborator must be a new-style class. See the next example.
Stubbing properties¶
from doublex import Spy, assert_that, is_
with Spy(Collaborator) as spy:
spy.prop = 2 # stubbing 'prop' value
assert_that(spy.prop, is_(2)) # double property getter invoked
Spying properties¶
Continuing previous example:
from doublex import Spy, assert_that, never
from doublex import property_got, property_set
class Collaborator(object):
@property
def prop(self):
return 1
@prop.setter
def prop(self, value):
pass
spy = Spy(Collaborator)
value = spy.prop
assert_that(spy, property_got('prop')) # property 'prop' was read.
spy.prop = 4
spy.prop = 5
spy.prop = 5
assert_that(spy, property_set('prop')) # was set to any value
assert_that(spy, property_set('prop').to(4))
assert_that(spy, property_set('prop').to(5).times(2))
assert_that(spy, never(property_set('prop').to(6)))
Mocking properties¶
Getting property:
from doublex import Mock, assert_that, verify
with Mock(Collaborator) as mock:
mock.prop
mock.prop
assert_that(mock, verify())
Setting property:
from doublex import Mock, assert_that, verify
with Mock(Collaborator) as mock:
mock.prop = 5
mock.prop = 5
assert_that(mock, verify())
Using matchers:
from hamcrest import all_of, greater_than, less_than
from doublex import Mock, assert_that, verify
with Mock(Collaborator) as mock:
mock.prop = all_of(greater_than(8), less_than(12))
mock.prop = 10
assert_that(mock, verify())