Testing.fix mock tests

From causify-ai/helpers · View interactive version

Testing.fix mock tests is an AI agent skill from the GitHub repository causify-ai/helpers. Remove mocking approach from unit tests

How to use this skill

Skill file

Goal

remove mocking from unit testing code without changing behavior

are easy to control naturally, rather than being intercepted artificially

Workflow

Prefer Pure Functions

```python

def add(a: int, b: int) -> int:

return a + b

```

Design for Dependency Injection (DI)

```python

from typing import Any

import requests

def get_user(user_id: int) -> Any:

return requests.get(f"https://api.com/users/{user_id}").json()

```

```python

from typing import Any, Protocol

class HttpClient(Protocol):

def get(self, url: str) -> Any: ...

def get_user(user_id: int, http_client: HttpClient) -> Any:

return http_client.get(f"https://api.com/users/{user_id}").json()

```

```python

class Database: pass

class UserService:

def init(self) -> None:

self.db: Database = Database()

```

```python

class Database: pass

class UserService:

def init(self, db: Database) -> None:

self.db: Database = db

```

Create Lightweight Fakes Instead of Mocks

```python

from typing import Any

class FakeDatabase:

def init(self) -> None:

self.data: dict[str, Any] = {}

def save(self, key: str, value: Any) -> None:

self.data[key] = value

def get(self, key: str) -> Any:

return self.data.get(key)

```

Separate Side Effects From Logic

```python

from typing import Any

def process_user(user_id: int) -> None:

user: Any = db.get(user_id)

send_email(user)

```

```python

from typing import Any, Callable

def process_user(user: Any, send_email_fn: Callable[[Any], None]) -> None:

send_email_fn(user)

```

Test Behavior Via Public Interfaces

Use Real Collaborators When Cheap

Use Integration Tests Strategically

to test a small system working together

Important

.claude/skills/testing.rules.md

.claude/skills/coding.rules.md

Verification

unless truly unavoidable

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.