|
| 1 | +# Mock attributes |
| 2 | + |
| 3 | +Python [property attributes][] provide an interface for creating read-only properties and properties with getters, setters, and deleters. You can use Decoy to stub these properties. |
| 4 | + |
| 5 | +[property attributes]: https://docs.python.org/3/library/functions.html#property |
| 6 | + |
| 7 | +## Default behavior |
| 8 | + |
| 9 | +Unlike mock method calls - which have a default return value of `None` - Decoy's default return value for attribute access is **another mock**. So you don't need to configure anything explicitly if you need a `@property` getter to return another mock; Decoy will do this for you. |
| 10 | + |
| 11 | +```python |
| 12 | +class Child: |
| 13 | + ... |
| 14 | + |
| 15 | +class Parent: |
| 16 | + @property |
| 17 | + def child(self) -> Child: |
| 18 | + ... |
| 19 | + |
| 20 | +mock_parent = decoy.mock(cls=Parent) |
| 21 | + |
| 22 | +assert isinstance(mock_parent, Dependency) |
| 23 | +assert isinstance(mock_parent.child, Child) |
| 24 | +``` |
| 25 | + |
| 26 | +You may also manually set any mock attribute to "opt out" of Decoy for a given attribute. To opt back into Decoy, delete the attribute. |
| 27 | + |
| 28 | +```python |
| 29 | +dep = decoy.mock(cls=Parent) |
| 30 | + |
| 31 | +dep.child = "don't worry about it" |
| 32 | +assert dep.child == "don't worry about it" |
| 33 | + |
| 34 | +del dep.child |
| 35 | +assert isinstance(mock_parent.child, Child) |
| 36 | +``` |
| 37 | + |
| 38 | +## Stub property access |
| 39 | + |
| 40 | +### Stub a getter |
| 41 | + |
| 42 | +If you would like to stub a return value for a property that is different than the default behavior, simply use [`When.get`][decoy.next.When.get]. |
| 43 | + |
| 44 | +```python |
| 45 | +dependency = decoy.mock(name="dependency") |
| 46 | + |
| 47 | +decoy.when(dependency.some_property).get().then_return(42) |
| 48 | + |
| 49 | +assert dep.some_property == 42 |
| 50 | +``` |
| 51 | + |
| 52 | +You can also configure any other behavior, like raising an error. |
| 53 | + |
| 54 | +```python |
| 55 | +dependency = decoy.mock(name="dependency") |
| 56 | + |
| 57 | +decoy |
| 58 | + .when(dependency.some_property) |
| 59 | + .get() |
| 60 | + .then_raise(RuntimeError("oh no")) |
| 61 | + |
| 62 | +with pytest.raises(RuntimeError, match="oh no"): |
| 63 | + dependency.some_property |
| 64 | +``` |
| 65 | + |
| 66 | +### Stub a setter or deleter |
| 67 | + |
| 68 | +While you cannot stub a return value for a getter or setter, you can stub a `raise` or a side effect with [`When.set`][decoy.next.When.set] and [`When.delete`][decoy.next.When.delete]. |
| 69 | + |
| 70 | +```python |
| 71 | +dependency = decoy.mock(name="dependency") |
| 72 | + |
| 73 | +decoy |
| 74 | + .when(dependency.some_property) |
| 75 | + .set(42) |
| 76 | + .then_raise(RuntimeError("oh no")) |
| 77 | + |
| 78 | +decoy |
| 79 | + .when(dependency.some_property) |
| 80 | + .delete() |
| 81 | + .then_raise(RuntimeError("what a disaster")) |
| 82 | + |
| 83 | +with pytest.raises(RuntimeError, match="oh no"): |
| 84 | + dependency.some_property = 42 |
| 85 | + |
| 86 | +with pytest.raises(RuntimeError, match="what a disaster"): |
| 87 | + del dependency.some_property |
| 88 | +``` |
| 89 | + |
| 90 | +!!! tip |
| 91 | + |
| 92 | + You cannot use [`then_return`][decoy.Stub.then_return] with attribute setters and deleters, because set and delete expressions do not return a value in Python. |
| 93 | + |
| 94 | +## Verify property access |
| 95 | + |
| 96 | +You can verify calls to property setters and deleters with [`Verify.set`][decoy.next.Verify.set] and [`Verify.delete`][decoy.next.Verify.delete]. |
| 97 | + |
| 98 | +!!! tip |
| 99 | + |
| 100 | + Use this feature sparingly! If you're designing a dependency that triggers a side-effect, consider using a regular method, instead. |
| 101 | + |
| 102 | + Mocking and verifying property setters and deleters is most useful for testing code that needs to interact with older or legacy dependencies that would be prohibitively expensive to redesign. |
| 103 | + |
| 104 | +### Verify a setter |
| 105 | + |
| 106 | +Use [`Verify.set`][decoy.next.Verify.set] to check that an attribute was set. |
| 107 | + |
| 108 | +```python |
| 109 | +dependency = decoy.mock(name="dependency") |
| 110 | + |
| 111 | +dependency.some_property = 42 |
| 112 | + |
| 113 | +decoy.verify(dependency.some_property).set(42) |
| 114 | +``` |
| 115 | + |
| 116 | +### Verify a deleter |
| 117 | + |
| 118 | +Use [`Verify.delete`][decoy.next.Verify.delete] to check that an attribute was deleted. |
| 119 | + |
| 120 | +```python |
| 121 | +dependency = decoy.mock(name="dependency") |
| 122 | + |
| 123 | +del dependency.some_property |
| 124 | + |
| 125 | +decoy.verify(dependency.some_property).delete) |
| 126 | +``` |
0 commit comments