# Mocking Strategies {#mocking-strategies}


Effective mocking patterns for Gauzy tests.


## Repository Mocking {#repository-mocking}


```typescript
const mockRepository = {
  findOne: jest.fn(),
  find: jest.fn(),
  save: jest.fn(),
  delete: jest.fn(),
  count: jest.fn(),
  createQueryBuilder: jest.fn(() => ({
    where: jest.fn().mockReturnThis(),
    andWhere: jest.fn().mockReturnThis(),
    getMany: jest.fn().mockResolvedValue([]),
    getCount: jest.fn().mockResolvedValue(0),
  })),
};
```


## Service Mocking {#service-mocking}


```typescript
const mockEmployeeService = {
  findAll: jest.fn().mockResolvedValue({ items: [], total: 0 }),
  findOneByIdString: jest.fn(),
  create: jest.fn(),
  update: jest.fn(),
  delete: jest.fn(),
};

// In module
providers: [{ provide: EmployeeService, useValue: mockEmployeeService }];
```


## HTTP Mocking {#http-mocking}


```typescript
// Angular HTTP testing
import {
  HttpClientTestingModule,
  HttpTestingController,
} from "@angular/common/http/testing";

let httpMock: HttpTestingController;

it("should fetch employees", () => {
  service.getEmployees().subscribe((data) => {
    expect(data.length).toBe(2);
  });

  const req = httpMock.expectOne("/api/employee");
  req.flush([{ id: "1" }, { id: "2" }]);
});
```


## Request Context Mocking {#request-context-mocking}


```typescript
const mockRequest = {
  user: { id: "user-uuid", tenantId: "tenant-uuid" },
  headers: { authorization: "Bearer token" },
};

const mockRequestContext = {
  currentTenantId: jest.fn().mockReturnValue("tenant-uuid"),
  currentUserId: jest.fn().mockReturnValue("user-uuid"),
};
```


## When to Mock vs. Use Real {#when-to-mock-vs-use-real}


| Use Mock               | Use Real                    |
| ---------------------- | --------------------------- |
| External HTTP APIs     | Database (in integration)   |
| Email services         | Validation pipes            |
| File storage providers | DTO transformations         |
| Payment gateways       | Business logic calculations |


## Related Pages {#related-pages}


- [Unit Testing Guide](https://sourcey-gauzy.pages.dev/testing/unit-testing/index.md) — unit tests
- [Test Fixtures](https://sourcey-gauzy.pages.dev/testing/test-fixtures/index.md) — test data
- [Testing Guards](https://sourcey-gauzy.pages.dev/testing/testing-guards/index.md) — guard tests
