File

testing/src/logging.mock.ts

Description

Mock class of the StarkLoggingService interface.

Implements

SpyObj

Index

Properties

Constructor

Public constructor(mockCorrelationId: string, mockCorrelationIdHeaderName: string)

Creates a new mock instance.

Parameters :
Name Type Optional Description
mockCorrelationId string No
  • Correlation id to set to this instance
mockCorrelationIdHeaderName string No
  • Correlation header name to set to this instance

Properties

Public correlationId
Public correlationIdHttpHeaderName
Public debug
Type : Spy<>
Default value : createSpy("debug")
Public error
Type : Spy<>
Default value : createSpy("error")
Public generateNewCorrelationId
Type : Spy<>
Default value : createSpy("generateNewCorrelationId")
Public info
Type : Spy<>
Default value : createSpy("info")
Public warn
Type : Spy<>
Default value : createSpy("warn")

Usage

The mock class MockStarkLoggingService can be imported as follows:

import { MockStarkLoggingService } from "@nationalbankbelgium/stark-core/testing";

Since the mock class implements the base interface of the service it mocks, you just need to provide the mock in your TestingModule:

TestBed.configureTestingModule({
    imports: [...],
    declarations: [...],
    providers: [
        ...
        { provide: STARK_LOGGING_SERVICE, useValue: new MockStarkLoggingService() },
        ...
    ]
});

The instantiated mock will have these default properties assigned:

  • correlationId = "dummyCorrelationId"
  • correlationIdHttpHeaderName = "Correlation-Id-HttpHeaderName".

In case you need specific values for any of those properties, then you need to pass a custom correlationId and/or a custom correlationIdHttpHeaderName to the constructor of the mock class:

new MockStarkLoggingService("custom-correlation-id", "custom-correlation-id-header");

Then you can just inject the Stark service via the TestBed using its corresponding InjectionToken:

// this will inject the instantiated mock class
mockLoggingService = TetBed.get(STARK_LOGGING_SERVICE);

In fact, every method of the base interface is simply mocked with a Jasmine Spy which can then be used in the unit tests to:

  • return custom values
  • override a method with a custom function
  • asserting that they are actually called
  • do any other operation than can be performed with an Spy.

For example:

// reading a value
const correlationId = mockLoggingService.correlationId;

// overriding a method with a custom function
mockLoggingService.error.and.callFake((someMessage, someError) => {
  // some custom logic to return a specific value
});

// asserting that a method was indeed called
expect(mockLoggingService.error).toHaveBeenCalledTimes(1);
expect(mockLoggingService.error).toHaveBeenCalledWith(someMessage, someError);

import { StarkLoggingService } from "@nationalbankbelgium/stark-core";
import Spy = jasmine.Spy;
import SpyObj = jasmine.SpyObj;
import createSpy = jasmine.createSpy;

/**
 * Mock class of the {@link StarkLoggingService} interface.
 */
export class MockStarkLoggingService implements SpyObj<StarkLoggingService> {
	/**
	 * See [StarkLoggingService correlationId]{@link StarkLoggingService#correlationId} property
	 */
	public correlationId: StarkLoggingService["correlationId"];

	/**
	 * See [StarkLoggingService correlationIdHttpHeaderName]{@link StarkLoggingService#correlationIdHttpHeaderName} property
	 */
	public correlationIdHttpHeaderName: StarkLoggingService["correlationIdHttpHeaderName"];

	/**
	 * See [StarkLoggingService generateNewCorrelationId()]{@link StarkLoggingService#generateNewCorrelationId} method
	 */
	public generateNewCorrelationId: Spy<StarkLoggingService["generateNewCorrelationId"]> = createSpy("generateNewCorrelationId");

	/**
	 * See [StarkLoggingService debug()]{@link StarkLoggingService#debug} method
	 */
	public debug: Spy<StarkLoggingService["debug"]> = createSpy("debug");

	/**
	 * See [StarkLoggingService info()]{@link StarkLoggingService#info} method
	 */
	public info: Spy<StarkLoggingService["info"]> = createSpy("info");

	/**
	 * See [StarkLoggingService warn()]{@link StarkLoggingService#warn} method
	 */
	public warn: Spy<StarkLoggingService["warn"]> = createSpy("warn");

	/**
	 * See [StarkLoggingService error()]{@link StarkLoggingService#error} method
	 */
	public error: Spy<StarkLoggingService["error"]> = createSpy("error");

	/**
	 * Creates a new mock instance.
	 * @param mockCorrelationId - Correlation id to set to this instance
	 * @param mockCorrelationIdHeaderName - Correlation header name to set to this instance
	 */
	public constructor(
		mockCorrelationId: string = "dummyCorrelationId",
		mockCorrelationIdHeaderName: string = "Correlation-Id-HttpHeaderName"
	) {
		this.correlationId = mockCorrelationId;
		this.correlationIdHttpHeaderName = mockCorrelationIdHeaderName;
	}
}

result-matching ""

    No results matching ""