Skip to content

Commit 7a5b62b

Browse files
committed
replaced stub with mock terminology
1 parent 3dfd64a commit 7a5b62b

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

README.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ We also have a RedisService which connects to a Redis server and performs a few
1313

1414
The purpose of this project is to:
1515
1. Demonstrate how specmatic can be used to validate the contract of the bff service
16-
while stubbing out the order api service at the same time.
16+
while mocking out the order api service at the same time.
1717

18-
2. Demonstrate how to stub out the Redis server using Specmatic Redis and Test Containers.
18+
2. Demonstrate how to mock out the Redis server using Specmatic Redis Mock and Test Containers.
1919

2020

2121
## Prerequisites
@@ -39,9 +39,9 @@ Run the tests using pytest:
3939
pytest tests -v -s
4040
```
4141

42-
## Setting stubs for Specmatic Redis
43-
Specmatic Redis takes an argument 'data' which is expected to be a directory with stub json files.
44-
The stub files are expected to have this structure:
42+
## Setting test data for Specmatic Redis
43+
Specmatic Redis takes an argument 'data' which is expected to be a directory with test data json files.
44+
The test data files are expected to have this structure:
4545

4646
```
4747
{
@@ -67,7 +67,7 @@ The stub files are expected to have this structure:
6767

6868
## Running Specmatic Redis in Tests (with Testcontainers)
6969

70-
Use the containerized Specmatic Redis Mock to spin up an ephemeral Redis-like stub for your tests. The snippet below starts the image, mounts your stub data, exposes the port, and **waits until the mock is ready** (detected via a log line) before the test proceeds.
70+
Use the containerized Specmatic Redis Mock to spin up an ephemeral Redis-like mock for your tests. The snippet below starts the image, mounts your test data, exposes the port, and **waits until the mock is ready** (detected via a log line) before the test proceeds.
7171

7272
```python
7373
from testcontainers.core.container import DockerContainer
@@ -77,23 +77,23 @@ from testcontainers.core.wait_strategies import LogMessageWaitStrategy
7777
SPECMATIC_REDIS_VERSION = "latest" # or a pinned version like "0.9.4"
7878
REDIS_HOST = "0.0.0.0"
7979
REDIS_PORT = 6379
80-
STUB_DATA_DIR = "/absolute/path/to/test/data"
80+
TEST_DATA_DIR = "/absolute/path/to/test/data"
8181

8282
container = (
8383
DockerContainer(f"specmatic/specmatic-redis:{SPECMATIC_REDIS_VERSION}")
84-
.with_command(f"virtualize --host {REDIS_HOST} --port {REDIS_PORT} --data {STUB_DATA_DIR}")
84+
.with_command(f"virtualize --host {REDIS_HOST} --port {REDIS_PORT} --data {TEST_DATA_DIR}")
8585
.with_exposed_ports(REDIS_PORT)
86-
.with_volume_mapping(STUB_DATA_DIR, STUB_DATA_DIR)
86+
.with_volume_mapping(TEST_DATA_DIR, TEST_DATA_DIR)
8787
.waiting_for(LogMessageWaitStrategy(r"Specmatic Redis has started on .*:\d+").with_startup_timeout(10))
8888
)
8989
```
9090

9191
### What this does
9292

9393
* **Image**: `specmatic/specmatic-redis:{SPECMATIC_REDIS_VERSION}` – pulls and runs the Specmatic Redis Mock.
94-
* **Command**: `virtualize --host ... --port ... --data ...` – launches Specmatic Redis and points it to your **stub dataset** (files that define responses/fixtures).
94+
* **Command**: `virtualize --host ... --port ... --data ...` – launches Specmatic Redis and points it to your **test dataset** (files that define responses/fixtures).
9595
* **Port exposure**: `.with_exposed_ports(REDIS_PORT)` – publishes the Redis port to the host so your test client can connect.
96-
* **Volume mapping**: `.with_volume_mapping(STUB_DATA_DIR, STUB_DATA_DIR)` – mounts your local stub directory into the container at the **same** path (keeps file references simple).
96+
* **Volume mapping**: `.with_volume_mapping(TEST_DATA_DIR, TEST_DATA_DIR)` – mounts your local test data directory into the container at the **same** path (keeps file references simple).
9797
* **Readiness check**: `LogMessageWaitStrategy(...)` – blocks test execution until the container logs the **ready** line.
9898

9999
### Readiness log pattern
@@ -104,7 +104,7 @@ The regex `r"Specmatic Redis has started on .*:\d+"` matches a line like:
104104
Specmatic Redis has started on 0.0.0.0:6379
105105
```
106106

107-
Ensure `STUB_DATA_DIR` is an **absolute path** and contains your stub files.
107+
Ensure `TEST_DATA_DIR` is an **absolute path** and contains your test data files.
108108

109109

110110
## Running Contract Tests
@@ -115,7 +115,7 @@ Here’s a clean, professional README-ready write-up you can use to document tha
115115
## 🧪 Running Contract Tests with Specmatic (FastAPI Example)
116116

117117
This section demonstrates how to **run contract tests** for a FastAPI application using [Specmatic](https://specmatic.io/).
118-
Specmatic validates your app against its **OpenAPI contracts** and provides **API coverage reporting** and **stubbed interactions** for end-to-end testing.
118+
Specmatic validates your app against its **OpenAPI contracts** and provides **API coverage reporting** and **mock interactions** for end-to-end testing.
119119

120120
```python
121121
import os
@@ -126,9 +126,9 @@ from app.main import app as fastapi_app
126126
APP_HOST = "127.0.0.1"
127127
APP_PORT = 8000
128128

129-
STUB_HOST = "127.0.0.1"
130-
STUB_PORT = 8080
131-
STUB_DATA_DIR = ROOT_DIR + "tests/contract/data"
129+
MOCK_HOST = "127.0.0.1"
130+
MOCK_PORT = 8080
131+
TEST_DATA_DIR = ROOT_DIR + "tests/contract/data"
132132

133133
SPECMATIC_CONFIG_FILE_PATH = ROOT_DIR + '/specmatic.yaml'
134134

@@ -142,7 +142,7 @@ class TestContract:
142142
(
143143
Specmatic()
144144
.with_specmatic_config_file_path(SPECMATIC_CONFIG_FILE_PATH)
145-
.with_stub(STUB_HOST, STUB_PORT, args=[f"--data={STUB_DATA_DIR}"])
145+
.with_stub(MOCK_HOST, MOCK_PORT, args=[f"--data={TEST_DATA_DIR}"])
146146
.with_asgi_app('app.main:app', APP_HOST, APP_PORT)
147147
.test_with_api_coverage_for_fastapi_app(TestContract, fastapi_app)
148148
.run()
@@ -156,12 +156,12 @@ os.environ["SPECMATIC_GENERATIVE_TESTS"] = "false"
156156
### ⚙️ What This Does
157157

158158
| Step | Description |
159-
| ---------------------------------- |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
160-
| **Environment setup** | Defines the host and port for both the **FastAPI app** and the **Specmatic stub**. The stub uses data from `tests/contract/data` to simulate downstream dependencies. |
161-
| **Specmatic configuration** | Points to your central `specmatic.yaml` file, which defines service contracts and stub mappings. |
159+
|------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
160+
| **Environment setup** | Defines the host and port for both the **FastAPI app** and the **Specmatic mock**. The mock uses data from `tests/contract/data` to simulate downstream dependencies. |
161+
| **Specmatic configuration** | Points to your central `specmatic.yaml` file, which defines service contracts and dependencies. |
162162
| **Generative tests mode** | Enables Specmatic’s *generative testing* mode (`SPECMATIC_GENERATIVE_TESTS=true`) to generate resilience tests. |
163163
| **ASGI app registration** | `.with_asgi_app('app.main:app', APP_HOST, APP_PORT)` registers your FastAPI app for validation. Here, `app.main:app` refers to the `app` object inside the `main.py` module. |
164-
| **Stub registration** | `.with_stub(STUB_HOST, STUB_PORT, args=[f"--data={STUB_DATA_DIR}"])` launches the Specmatic stub locally to serve mock responses from the specified stub data directory. |
164+
| **Mock registration** | `.with_stub(MOCK_HOST, MOCK_PORT, args=[f"--data={TEST_DATA_DIR}"])` launches the Specmatic mock locally to serve mock responses from the specified test data directory. |
165165
| **Contract testing with coverage** | `.test_with_api_coverage_for_fastapi_app(TestContract, fastapi_app)` runs Specmatic tests against your FastAPI app and reports API coverage by spinning up a lightweight coverage server (which parts of the contract were exercised). |
166166
| **Clean-up** | Once the tests complete, the environment variable is reset to disable generative testing. |
167167

@@ -170,14 +170,14 @@ os.environ["SPECMATIC_GENERATIVE_TESTS"] = "false"
170170
### 🧩 Typical Workflow
171171

172172
1. Ensure your `specmatic.yaml` file points to the correct contract specs.
173-
2. Place your stubbed response files under:
173+
2. Place your test data files under:
174174

175175
```
176176
tests/contract/data/
177177
```
178178
3. Specmatic will:
179179

180-
* Start the stub server.
180+
* Start the mock server.
181181
* Boot your FastAPI app.
182182
* Execute contract-based test cases.
183183
* Report mismatches and coverage results.
@@ -186,7 +186,7 @@ os.environ["SPECMATIC_GENERATIVE_TESTS"] = "false"
186186
### 🧠 Notes
187187
* `app.main:app` follows the same syntax as `uvicorn main:app` — it references the `app` object defined inside `main.py`.
188188
* `SPECMATIC_GENERATIVE_TESTS=true` enables generation of resilience tests.
189-
* Keep `STUB_DATA_DIR` paths **absolute** to avoid issues with volume mounts or relative references.
189+
* Keep `TEST_DATA_DIR` paths **absolute** to avoid issues with volume mounts or relative references.
190190
* API coverage output helps ensure all contract endpoints are implemented and validated.
191191
---
192192

tests/redis/test_redis_service.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010

1111
REDIS_HOST = "0.0.0.0"
1212
REDIS_PORT = 6379
13-
STUB_DATA_DIR = ROOT_DIR + "/tests/redis/data"
13+
TEST_DATA_DIR = ROOT_DIR + "/tests/redis/data"
1414

1515
logger = logging.getLogger("specmatic.redis.mock")
1616
logger.setLevel(logging.DEBUG)
1717

18-
SPECMATIC_REDIS_VERSION = '0.9.4'
18+
SPECMATIC_REDIS_VERSION = 'latest'
1919

2020
class TestRedisService:
2121

2222
@pytest.fixture(scope="class")
2323
def redis_service(self):
2424
container = (
2525
DockerContainer(f"specmatic/specmatic-redis:{SPECMATIC_REDIS_VERSION}")
26-
.with_command(f"virtualize --host {REDIS_HOST} --port {REDIS_PORT} --data {STUB_DATA_DIR}")
26+
.with_command(f"virtualize --host {REDIS_HOST} --port {REDIS_PORT} --data {TEST_DATA_DIR}")
2727
.with_exposed_ports(REDIS_PORT)
28-
.with_volume_mapping(STUB_DATA_DIR, STUB_DATA_DIR)
28+
.with_volume_mapping(TEST_DATA_DIR, TEST_DATA_DIR)
2929
.waiting_for(LogMessageWaitStrategy(r"Specmatic Redis has started on .*:\d+").with_startup_timeout(10))
3030
)
3131

0 commit comments

Comments
 (0)