agentleFS
girijashankarjgirijashankarj/cursor-handbook/.cursor/rules/testing/mock-patterns.mdc

cursor-handbook / testing

Cursor rule31 starsChanged 3 months agoMITView on GitHub

Mock and test fixture patterns for consistent testing

The file

We split this mock-patterns.mdc into the rules it gives an agent

5 rules. About 0 are common, 5 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈5

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    NEVER: use real API endpoints in tests

    line 77
  2. Distinctive:

    NEVER: use real database connections in unit tests

    line 78
  3. Distinctive:

    NEVER: use real credentials in test fixtures

    line 79
  4. Distinctive:

    Use factories for dynamic test data, fixtures for static data

    line 81
  5. Distinctive:

    Keep mock implementations simple — only mock what's needed

    line 82

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
mock-patterns.mdc
---
description: "Mock and test fixture patterns for consistent testing"
alwaysApply: true
---

# Mock Patterns

## Centralized Mock Infrastructure
```
tests/
├── mocks/
│   ├── services/           # Service mocks
│   │   ├── database.mock.ts
│   │   ├── cache.mock.ts
│   │   └── queue.mock.ts
│   ├── factories/          # Test data factories
│   │   ├── order.factory.ts
│   │   ├── user.factory.ts
│   │   └── product.factory.ts
│   ├── fixtures/           # Static test data
│   │   ├── orders.json
│   │   └── users.json
│   └── setup.ts            # Global test setup
├── helpers/
│   ├── test-server.ts      # Test server utilities
│   └── assertions.ts       # Custom assertions
└── integration/
    └── setup.ts            # Integration test setup
```

## Factory Pattern
```{{CONFIG.techStack.language}}
// tests/mocks/factories/order.factory.ts
import { faker } from '@faker-js/faker';

interface OrderOverrides {
  id?: string;
  status?: string;
  total?: number;
}

export function createOrder(overrides: OrderOverrides = {}) {
  return {
    id: overrides.id ?? faker.string.uuid(),
    status: overrides.status ?? 'PENDING',
    total: overrides.total ?? faker.number.float({ min: 1, max: 1000 }),
    {{CONFIG.database.timestampFields.created}}: new Date().toISOString(),
    {{CONFIG.database.timestampFields.updated}}: new Date().toISOString(),
    {{CONFIG.database.softDeleteField}}: true,
    ...overrides,
  };
}

export function createOrders(count: number, overrides: OrderOverrides = {}) {
  return Array.from({ length: count }, () => createOrder(overrides));
}
```

## Service Mock Pattern
```{{CONFIG.techStack.language}}
// tests/mocks/services/database.mock.ts
export const mockDatabase = {
  query: jest.fn(),
  transaction: jest.fn(),
  connect: jest.fn(),
  disconnect: jest.fn(),
  reset() {
    this.query.mockReset();
    this.transaction.mockReset();
    this.connect.mockReset();
    this.disconnect.mockReset();
  }
};
```

## Rules
- **NEVER** use real API endpoints in tests
- **NEVER** use real database connections in unit tests
- **NEVER** use real credentials in test fixtures
- Reset all mocks in `beforeEach` or `afterEach`
- Use factories for dynamic test data, fixtures for static data
- Keep mock implementations simple — only mock what's needed

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.