|
| 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**. You don't need to configure anything explicitly if you need a `@property` getter to return another mock; Decoy will inspect type annotations and configure the proper child mock 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 attribute access |
| 39 | + |
| 40 | +### Stub a getter |
| 41 | + |
| 42 | +Use [`When.get`][decoy.next.When.get] stub a return value for an attribute instead of returning a child mock. |
| 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, because set and delete expressions do not return a value in Python, 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 | +## Verify property access |
| 91 | + |
| 92 | +You can verify calls to property setters and deleters with [`Verify.set`][decoy.next.Verify.set] and [`Verify.delete`][decoy.next.Verify.delete]. |
| 93 | + |
| 94 | +!!! tip |
| 95 | + |
| 96 | + Use this feature sparingly! If you're designing a dependency that triggers a side-effect, consider using a regular method, instead. |
| 97 | + |
| 98 | + 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. |
| 99 | + |
| 100 | +### Verify a setter |
| 101 | + |
| 102 | +Use [`Verify.set`][decoy.next.Verify.set] to check that an attribute was set. |
| 103 | + |
| 104 | +```python |
| 105 | +dependency = decoy.mock(name="dependency") |
| 106 | + |
| 107 | +dependency.some_property = 42 |
| 108 | + |
| 109 | +decoy.verify(dependency.some_property).set(42) |
| 110 | +``` |
| 111 | + |
| 112 | +### Verify a deleter |
| 113 | + |
| 114 | +Use [`Verify.delete`][decoy.next.Verify.delete] to check that an attribute was deleted. |
| 115 | + |
| 116 | +```python |
| 117 | +dependency = decoy.mock(name="dependency") |
| 118 | + |
| 119 | +del dependency.some_property |
| 120 | + |
| 121 | +decoy.verify(dependency.some_property).delete) |
| 122 | +``` |
0 commit comments