Skip to main content

TestBed.sociable()

Creates a test environment that mixes real implementations with mocked dependencies for testing interactions between business logic classes.

Sociable Tests Are Still Unit Tests

Even with multiple real classes, sociable tests remain unit tests because external I/O (databases, HTTP, caches) are injected via tokens (@Inject('DATABASE')) which are automatically mocked. You're testing business logic interactions, not actual I/O. See the Sociable Unit Tests Guide for concepts.

Signature

TestBed.sociable<T>(targetClass: Type<T>): SociableTestBuilder<T>

Parameters

ParameterTypeDescription
targetClassType<T>The class constructor to test

Returns

SociableTestBuilder<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>): SociableTestBuilder<T>
  • .expose() only accepts class constructors, not tokens. Tokens represent abstractions (interfaces, types) following the Dependency Inversion Principle - there is no concrete implementation to "expose". Additionally, tokens typically represent external I/O (databases, HTTP clients) and are always mocked to keep sociable tests fast and side-effect-free.

  • .expose() method only controls explicit, injected dependencies. Implicit dependencies (direct imports) are not intercepted by TestBed and 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 real

What's Retrievable

With .expose():

  • ✅ Non-exposed dependencies (mocked by default)
  • ✅ Tokens (auto-mocked)
  • ✅ Explicitly mocked dependencies
  • ❌ Exposed dependencies (real, not retrievable)

See Also