MCPcopy Index your code
hub / github.com/RustPython/RustPython / Mock

Class Mock

Lib/unittest/mock.py:1258–1314  ·  view source on GitHub ↗

Create a new `Mock` object. `Mock` takes several optional arguments that specify the behaviour of the Mock object: * `spec`: This can be either a list of strings or an existing object (a class or instance) that acts as the specification for the mock object. If you pass in a

Source from the content-addressed store, hash-verified

1256
1257
1258class Mock(CallableMixin, NonCallableMock):
1259 """
1260 Create a new `Mock` object. `Mock` takes several optional arguments
1261 that specify the behaviour of the Mock object:
1262
1263 * `spec`: This can be either a list of strings or an existing object (a
1264 class or instance) that acts as the specification for the mock object. If
1265 you pass in an object then a list of strings is formed by calling dir on
1266 the object (excluding unsupported magic attributes and methods). Accessing
1267 any attribute not in this list will raise an `AttributeError`.
1268
1269 If `spec` is an object (rather than a list of strings) then
1270 `mock.__class__` returns the class of the spec object. This allows mocks
1271 to pass `isinstance` tests.
1272
1273 * `spec_set`: A stricter variant of `spec`. If used, attempting to *set*
1274 or get an attribute on the mock that isn't on the object passed as
1275 `spec_set` will raise an `AttributeError`.
1276
1277 * `side_effect`: A function to be called whenever the Mock is called. See
1278 the `side_effect` attribute. Useful for raising exceptions or
1279 dynamically changing return values. The function is called with the same
1280 arguments as the mock, and unless it returns `DEFAULT`, the return
1281 value of this function is used as the return value.
1282
1283 If `side_effect` is an iterable then each call to the mock will return
1284 the next value from the iterable. If any of the members of the iterable
1285 are exceptions they will be raised instead of returned.
1286
1287 * `return_value`: The value returned when the mock is called. By default
1288 this is a new Mock (created on first access). See the
1289 `return_value` attribute.
1290
1291 * `unsafe`: By default, accessing any attribute whose name starts with
1292 *assert*, *assret*, *asert*, *aseert*, or *assrt* raises an AttributeError.
1293 Additionally, an AttributeError is raised when accessing
1294 attributes that match the name of an assertion method without the prefix
1295 `assert_`, e.g. accessing `called_once` instead of `assert_called_once`.
1296 Passing `unsafe=True` will allow access to these attributes.
1297
1298 * `wraps`: Item for the mock object to wrap. If `wraps` is not None then
1299 calling the Mock will pass the call through to the wrapped object
1300 (returning the real result). Attribute access on the mock will return a
1301 Mock object that wraps the corresponding attribute of the wrapped object
1302 (so attempting to access an attribute that doesn't exist will raise an
1303 `AttributeError`).
1304
1305 If the mock has an explicit `return_value` set then calls are not passed
1306 to the wrapped object and the `return_value` is returned instead.
1307
1308 * `name`: If the mock has a name then it will be used in the repr of the
1309 mock. This can be useful for debugging. The name is propagated to child
1310 mocks.
1311
1312 Mocks can also be called with arbitrary keyword arguments. These will be
1313 used to set attributes on the mock after it is created.
1314 """
1315

Calls

no outgoing calls