TestBed.sociable()
Creates a test environment that mixes real implementations with mocked dependencies for testing interactions between business logic classes.
Signature
TestBed.sociable<T>(targetClass: Type<T>): SociableTestBedBuilder<T>Parameters
| Parameter | Type | Description |
|---|---|---|
| targetClass | Type<T> | The class constructor to test |
Returns
SociableTestBedBuilder<T> with the following configuration method:
.expose()- List classes to keep real. Everything else is mocked..mock(dependency)- Configure specific mock behavior before compilation.compile()- Finalizes configuration and creates the test environment
The .expose() Method
expose<D>(dependency: Type<D>): SociableTestBedBuilder<T>-
.expose()only accepts class constructors, not string, symbol, or other non-class tokens. Those tokens identify dependencies but do not supply a class implementation for Suites to instantiate. Token dependencies are mocked by default;.mock().final()can instead inject a supplied value or implementation. -
.expose()method only controls explicit, injected dependencies. Implicit dependencies (direct imports) are not intercepted byTestBedand will execute as normal.
Example
const { unit, unitRef } = await TestBed.sociable(UserService) .expose(UserValidator) // Only this is real .compile();
// Can retrieve non-exposed (mocked)const database = unitRef.get(Database);
// Cannot retrieve exposed// const validator = unitRef.get(UserValidator); // ERROR - it's realWhat’s Retrievable
With .expose():
- ✅ Non-exposed dependencies (mocked by default)
- ✅ Tokens (auto-mocked or configured with
.impl()) - ✅ Dependencies configured with
.mock().impl() - ❌ Exposed dependencies (real, not retrievable)
- ❌ Values supplied with
.mock().final()
See injected dependencies and retrievable mocks for how injection differs from unitRef lookup.
See Also
- Sociable Unit Tests Guide - When to use, concepts, detailed examples
- TestBed.solitary() - For fully isolated tests
- Mock Configuration - Configuring mock behavior