---
sidebar:
  label: roboto.testing.stub_roboto_client
  order: 1
title: roboto.testing.stub_roboto_client
---
## Module Contents

### RequestBodyMatcher

```python
roboto.testing.stub_roboto_client.RequestBodyMatcher
```

`from roboto.testing.stub_roboto_client import RequestBodyMatcher`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/testing/stub_roboto_client.py#L39-L39)

Function that takes a request's body, and returns whether the request matches an expectation.

See the `request_body_matcher` argument of `StubRobotoClient.expect_post`.

### StubResponse

```python
roboto.testing.stub_roboto_client.StubResponse
```

`from roboto.testing.stub_roboto_client import StubResponse`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/testing/stub_roboto_client.py#L22-L32)

Response a `StubRobotoClient` expectation answers a request with.

A pydantic model is returned in its JSON form and any other value as is, each as the `data` field of the response body. An exception is raised instead of returning a response.

### StubRobotoClient

```python
class roboto.testing.stub_roboto_client.StubRobotoClient
```

`from roboto.testing import StubRobotoClient`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/testing/stub_roboto_client.py#L67-L521)

Bases: [`roboto.http.RobotoClient`](/docs/reference/python-sdk/roboto/http/roboto_client#roboto.http.roboto_client.RobotoClient)

Client that answers SDK requests with responses a test registers, in place of Roboto.

Pass it as the `roboto_client` of the SDK methods under test; it sends nothing over the network. Register each request you expect, and the response to answer it with, by calling `expect_get`, `expect_post`, `expect_put`, `expect_patch` or `expect_delete`. Each request the client receives then:

- takes the first registered expectation that matches it, which no later request can match again;
- raises `AssertionError` when none matches, naming the request and the expectations still pending.

An expectation matches a request with the same method and path that also meets each condition the expectation sets: the same `query`, `caller_org_id` or `owner_org_id`, and a body its `request_body_matcher` accepts.

Used as a context manager, the client calls `verify_no_pending_expectations` when the `with` block ends, unless the block raised.

#### StubRobotoClient.delete()

```python
def delete(
    path: collections.abc.Sequence[str] | str,
    caller_org_id: Optional[str] = None,
    data: Any = None,
    headers: Optional[dict[str, str]] = None,
    idempotent: bool = True,
    owner_org_id: Optional[str] = None,
    query: Optional[dict[str, Any]] = None,
    retry_wait_fn: Optional[Any] = None,
    timeout: Any = None,
) -> roboto.http.response.HttpResponse
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/testing/stub_roboto_client.py#L409-L445)

Answer a DELETE request from the first registered expectation that matches it, without sending it.

The SDK's own code uses it; to choose the response to a DELETE request, use `expect_delete` instead.

**Parameters**

- **path** (`collections.abc.Sequence[str] | str`): Route of the request, as a string or as segments joined with `/`.
- **caller_org_id** (`Optional[str]`): Organization ID matched against each expectation's `caller_org_id`.
- **data** (`Any`): Request body, passed to each expectation's `request_body_matcher`.
- **headers** (`Optional[dict[str, str]]`): Ignored.
- **idempotent** (`bool`): Ignored.
- **owner_org_id** (`Optional[str]`): Organization ID matched against each expectation's `owner_org_id`.
- **query** (`Optional[dict[str, Any]]`): Query parameters matched against each expectation's `query`.
- **retry_wait_fn** (`Optional[Any]`): Ignored.
- **timeout** (`Any`): Ignored.

**Returns**

- `roboto.http.response.HttpResponse`: Response with status 200 whose JSON body holds the expectation's response in its `data` field.

**Raises**

- `AssertionError`: No pending expectation matches the request.
- `Exception`: The response registered for the matching expectation, when it is an exception.

#### StubRobotoClient.expect_delete()

```python
def expect_delete(
    path: str,
    response: StubResponse,
    query: Optional[dict[str, Any]] = None,
    request_body_matcher: Optional[RequestBodyMatcher] = None,
    caller_org_id: Optional[str] = None,
) -> StubRobotoClient
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/testing/stub_roboto_client.py#L231-L257)

Register an expected DELETE request and the response to answer it with.

**Parameters**

- **path** (`str`): Route the request is sent to, such as `"v1/datasets"`. A leading `/` is optional.
- **response** (`StubResponse`): Value to return as the `data` field of the response body, or an exception to raise instead. A pydantic model is returned in its JSON form.
- **query** (`Optional[dict[str, Any]]`): Query parameters the request must pass, compared as a whole dictionary. `None` matches any.
- **request_body_matcher** (`Optional[RequestBodyMatcher]`): Function called with the request's body, as the SDK method passed it (such as a pydantic request model), that returns whether the request matches. `None` matches any body.
- **caller_org_id** (`Optional[str]`): Organization ID the request must pass as `caller_org_id`. `None` matches any, including none.

**Returns**

- `StubRobotoClient`: This client, so that calls can be chained.

#### StubRobotoClient.expect_get()

```python
def expect_get(
    path: str,
    response: StubResponse,
    query: Optional[dict[str, Any]] = None,
    owner_org_id: Optional[str] = None,
    caller_org_id: Optional[str] = None,
) -> StubRobotoClient
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/testing/stub_roboto_client.py#L99-L145)

Register an expected GET request and the response to answer it with.

**Parameters**

- **path** (`str`): Route the request is sent to, such as `"v1/datasets/ds_12345"`. A leading `/` is optional.
- **response** (`StubResponse`): Value to return as the `data` field of the response body, or an exception to raise instead. A pydantic model is returned in its JSON form.
- **query** (`Optional[dict[str, Any]]`): Query parameters the request must pass, compared as a whole dictionary. `None` matches any.
- **owner_org_id** (`Optional[str]`): Organization ID the request must pass as `owner_org_id`. `None` matches any, including none.
- **caller_org_id** (`Optional[str]`): Organization ID the request must pass as `caller_org_id`. `None` matches any, including none.

**Returns**

- `StubRobotoClient`: This client, so that calls can be chained.

**Usage**

Answer the request [`from_id()`](/docs/reference/python-sdk/roboto/domain/datasets/dataset#roboto.domain.datasets.dataset.Dataset.from_id) sends:

```python
from roboto.domain.datasets import Dataset, DatasetRecord
from roboto.testing import StubRobotoClient
from roboto.time import utcnow
record = DatasetRecord(
    dataset_id="ds_12345",
    created=utcnow(),
    created_by="user:test@example.com",
    modified=utcnow(),
    modified_by="user:test@example.com",
    org_id="og_12345",
)
client = StubRobotoClient().expect_get("v1/datasets/ds_12345", response=record)
with client:
    dataset = Dataset.from_id("ds_12345", roboto_client=client)
dataset.dataset_id
# 'ds_12345'
```

#### StubRobotoClient.expect_patch()

```python
def expect_patch(
    path: str,
    response: StubResponse,
    query: Optional[dict[str, Any]] = None,
    request_body_matcher: Optional[RequestBodyMatcher] = None,
    caller_org_id: Optional[str] = None,
) -> StubRobotoClient
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/testing/stub_roboto_client.py#L203-L229)

Register an expected PATCH request and the response to answer it with.

**Parameters**

- **path** (`str`): Route the request is sent to, such as `"v1/datasets"`. A leading `/` is optional.
- **response** (`StubResponse`): Value to return as the `data` field of the response body, or an exception to raise instead. A pydantic model is returned in its JSON form.
- **query** (`Optional[dict[str, Any]]`): Query parameters the request must pass, compared as a whole dictionary. `None` matches any.
- **request_body_matcher** (`Optional[RequestBodyMatcher]`): Function called with the request's body, as the SDK method passed it (such as a pydantic request model), that returns whether the request matches. `None` matches any body.
- **caller_org_id** (`Optional[str]`): Organization ID the request must pass as `caller_org_id`. `None` matches any, including none.

**Returns**

- `StubRobotoClient`: This client, so that calls can be chained.

#### StubRobotoClient.expect_post()

```python
def expect_post(
    path: str,
    response: StubResponse,
    query: Optional[dict[str, Any]] = None,
    request_body_matcher: Optional[RequestBodyMatcher] = None,
    caller_org_id: Optional[str] = None,
) -> StubRobotoClient
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/testing/stub_roboto_client.py#L147-L173)

Register an expected POST request and the response to answer it with.

**Parameters**

- **path** (`str`): Route the request is sent to, such as `"v1/datasets"`. A leading `/` is optional.
- **response** (`StubResponse`): Value to return as the `data` field of the response body, or an exception to raise instead. A pydantic model is returned in its JSON form.
- **query** (`Optional[dict[str, Any]]`): Query parameters the request must pass, compared as a whole dictionary. `None` matches any.
- **request_body_matcher** (`Optional[RequestBodyMatcher]`): Function called with the request's body, as the SDK method passed it (such as a pydantic request model), that returns whether the request matches. `None` matches any body.
- **caller_org_id** (`Optional[str]`): Organization ID the request must pass as `caller_org_id`. `None` matches any, including none.

**Returns**

- `StubRobotoClient`: This client, so that calls can be chained.

#### StubRobotoClient.expect_put()

```python
def expect_put(
    path: str,
    response: StubResponse,
    query: Optional[dict[str, Any]] = None,
    request_body_matcher: Optional[RequestBodyMatcher] = None,
    caller_org_id: Optional[str] = None,
) -> StubRobotoClient
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/testing/stub_roboto_client.py#L175-L201)

Register an expected PUT request and the response to answer it with.

**Parameters**

- **path** (`str`): Route the request is sent to, such as `"v1/datasets"`. A leading `/` is optional.
- **response** (`StubResponse`): Value to return as the `data` field of the response body, or an exception to raise instead. A pydantic model is returned in its JSON form.
- **query** (`Optional[dict[str, Any]]`): Query parameters the request must pass, compared as a whole dictionary. `None` matches any.
- **request_body_matcher** (`Optional[RequestBodyMatcher]`): Function called with the request's body, as the SDK method passed it (such as a pydantic request model), that returns whether the request matches. `None` matches any body.
- **caller_org_id** (`Optional[str]`): Organization ID the request must pass as `caller_org_id`. `None` matches any, including none.

**Returns**

- `StubRobotoClient`: This client, so that calls can be chained.

#### StubRobotoClient.get()

```python
def get(
    path: collections.abc.Sequence[str] | str,
    caller_org_id: Optional[str] = None,
    headers: Optional[dict[str, str]] = None,
    idempotent: bool = True,
    owner_org_id: Optional[str] = None,
    query: Optional[dict[str, Any]] = None,
    retry_wait_fn: Optional[Any] = None,
    timeout: Any = None,
) -> roboto.http.response.HttpResponse
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/testing/stub_roboto_client.py#L259-L293)

Answer a GET request from the first registered expectation that matches it, without sending it.

The SDK's own code uses it; to choose the response to a GET request, use `expect_get` instead.

**Parameters**

- **path** (`collections.abc.Sequence[str] | str`): Route of the request, as a string or as segments joined with `/`.
- **caller_org_id** (`Optional[str]`): Organization ID matched against each expectation's `caller_org_id`.
- **headers** (`Optional[dict[str, str]]`): Ignored.
- **idempotent** (`bool`): Ignored.
- **owner_org_id** (`Optional[str]`): Organization ID matched against each expectation's `owner_org_id`.
- **query** (`Optional[dict[str, Any]]`): Query parameters matched against each expectation's `query`.
- **retry_wait_fn** (`Optional[Any]`): Ignored.
- **timeout** (`Any`): Ignored.

**Returns**

- `roboto.http.response.HttpResponse`: Response with status 200 whose JSON body holds the expectation's response in its `data` field.

**Raises**

- `AssertionError`: No pending expectation matches the request.
- `Exception`: The response registered for the matching expectation, when it is an exception.

#### StubRobotoClient.patch()

```python
def patch(
    path: collections.abc.Sequence[str] | str,
    caller_org_id: Optional[str] = None,
    data: Any = None,
    headers: Optional[dict[str, str]] = None,
    idempotent: bool = True,
    owner_org_id: Optional[str] = None,
    query: Optional[dict[str, Any]] = None,
    retry_wait_fn: Optional[Any] = None,
    timeout: Any = None,
) -> roboto.http.response.HttpResponse
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/testing/stub_roboto_client.py#L371-L407)

Answer a PATCH request from the first registered expectation that matches it, without sending it.

The SDK's own code uses it; to choose the response to a PATCH request, use `expect_patch` instead.

**Parameters**

- **path** (`collections.abc.Sequence[str] | str`): Route of the request, as a string or as segments joined with `/`.
- **caller_org_id** (`Optional[str]`): Organization ID matched against each expectation's `caller_org_id`.
- **data** (`Any`): Request body, passed to each expectation's `request_body_matcher`.
- **headers** (`Optional[dict[str, str]]`): Ignored.
- **idempotent** (`bool`): Ignored.
- **owner_org_id** (`Optional[str]`): Organization ID matched against each expectation's `owner_org_id`.
- **query** (`Optional[dict[str, Any]]`): Query parameters matched against each expectation's `query`.
- **retry_wait_fn** (`Optional[Any]`): Ignored.
- **timeout** (`Any`): Ignored.

**Returns**

- `roboto.http.response.HttpResponse`: Response with status 200 whose JSON body holds the expectation's response in its `data` field.

**Raises**

- `AssertionError`: No pending expectation matches the request.
- `Exception`: The response registered for the matching expectation, when it is an exception.

#### StubRobotoClient.post()

```python
def post(
    path: collections.abc.Sequence[str] | str,
    caller_org_id: Optional[str] = None,
    data: Any = None,
    headers: Optional[dict[str, str]] = None,
    idempotent: bool = True,
    owner_org_id: Optional[str] = None,
    query: Optional[dict[str, Any]] = None,
    retry_wait_fn: Optional[Any] = None,
    timeout: Any = None,
) -> roboto.http.response.HttpResponse
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/testing/stub_roboto_client.py#L295-L331)

Answer a POST request from the first registered expectation that matches it, without sending it.

The SDK's own code uses it; to choose the response to a POST request, use `expect_post` instead.

**Parameters**

- **path** (`collections.abc.Sequence[str] | str`): Route of the request, as a string or as segments joined with `/`.
- **caller_org_id** (`Optional[str]`): Organization ID matched against each expectation's `caller_org_id`.
- **data** (`Any`): Request body, passed to each expectation's `request_body_matcher`.
- **headers** (`Optional[dict[str, str]]`): Ignored.
- **idempotent** (`bool`): Ignored.
- **owner_org_id** (`Optional[str]`): Organization ID matched against each expectation's `owner_org_id`.
- **query** (`Optional[dict[str, Any]]`): Query parameters matched against each expectation's `query`.
- **retry_wait_fn** (`Optional[Any]`): Ignored.
- **timeout** (`Any`): Ignored.

**Returns**

- `roboto.http.response.HttpResponse`: Response with status 200 whose JSON body holds the expectation's response in its `data` field.

**Raises**

- `AssertionError`: No pending expectation matches the request.
- `Exception`: The response registered for the matching expectation, when it is an exception.

#### StubRobotoClient.put()

```python
def put(
    path: collections.abc.Sequence[str] | str,
    caller_org_id: Optional[str] = None,
    data: Any = None,
    headers: Optional[dict[str, str]] = None,
    idempotent: bool = True,
    owner_org_id: Optional[str] = None,
    query: Optional[dict[str, Any]] = None,
    retry_wait_fn: Optional[Any] = None,
    timeout: Any = None,
) -> roboto.http.response.HttpResponse
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/testing/stub_roboto_client.py#L333-L369)

Answer a PUT request from the first registered expectation that matches it, without sending it.

The SDK's own code uses it; to choose the response to a PUT request, use `expect_put` instead.

**Parameters**

- **path** (`collections.abc.Sequence[str] | str`): Route of the request, as a string or as segments joined with `/`.
- **caller_org_id** (`Optional[str]`): Organization ID matched against each expectation's `caller_org_id`.
- **data** (`Any`): Request body, passed to each expectation's `request_body_matcher`.
- **headers** (`Optional[dict[str, str]]`): Ignored.
- **idempotent** (`bool`): Ignored.
- **owner_org_id** (`Optional[str]`): Organization ID matched against each expectation's `owner_org_id`.
- **query** (`Optional[dict[str, Any]]`): Query parameters matched against each expectation's `query`.
- **retry_wait_fn** (`Optional[Any]`): Ignored.
- **timeout** (`Any`): Ignored.

**Returns**

- `roboto.http.response.HttpResponse`: Response with status 200 whose JSON body holds the expectation's response in its `data` field.

**Raises**

- `AssertionError`: No pending expectation matches the request.
- `Exception`: The response registered for the matching expectation, when it is an exception.

#### StubRobotoClient.verify_no_pending_expectations()

```python
def verify_no_pending_expectations() -> None
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/testing/stub_roboto_client.py#L447-L456)

Check that every registered expectation has answered a request.

**Raises**

- `AssertionError`: An expectation has answered no request. The message lists each such expectation's method and path.

**Returns**

- `None`
