testing/src/logging.mock.ts
Mock class of the StarkLoggingService interface.
SpyObj
Properties |
|
Public
constructor(mockCorrelationId: string, mockCorrelationIdHeaderName: string)
|
Defined in testing/src/logging.mock.ts:43
|
Creates a new mock instance. |
Public correlationId |
Defined in testing/src/logging.mock.ts:13
|
See StarkLoggingService correlationId property |
Public correlationIdHttpHeaderName |
Defined in testing/src/logging.mock.ts:18
|
See StarkLoggingService correlationIdHttpHeaderName property |
Public debug |
Type : Spy<>
|
Default value : createSpy("debug")
|
Defined in testing/src/logging.mock.ts:28
|
See StarkLoggingService debug() method |
Public error |
Type : Spy<>
|
Default value : createSpy("error")
|
Defined in testing/src/logging.mock.ts:43
|
See StarkLoggingService error() method |
Public generateNewCorrelationId |
Type : Spy<>
|
Default value : createSpy("generateNewCorrelationId")
|
Defined in testing/src/logging.mock.ts:23
|
Public info |
Type : Spy<>
|
Default value : createSpy("info")
|
Defined in testing/src/logging.mock.ts:33
|
See StarkLoggingService info() method |
Public warn |
Type : Spy<>
|
Default value : createSpy("warn")
|
Defined in testing/src/logging.mock.ts:38
|
See StarkLoggingService warn() method |
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 = TestBed.inject(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:
For example:
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;
}
}