testing/src/message-pane.mock.ts
Mock class of the StarkMessagePaneService interface.
SpyObj
Properties |
Public add |
Type : Spy<>
|
Default value : createSpy("add")
|
Defined in testing/src/message-pane.mock.ts:20
|
See StarkMessagePaneService add() method |
Public addOne |
Type : Spy<>
|
Default value : createSpy("addOne")
|
Defined in testing/src/message-pane.mock.ts:25
|
See StarkMessagePaneService addOne() method |
Public clearAll |
Type : Spy<>
|
Default value : createSpy("clearAll")
|
Defined in testing/src/message-pane.mock.ts:40
|
See StarkMessagePaneService clearAll() method |
Public clearOnNavigation |
Default value : false
|
Defined in testing/src/message-pane.mock.ts:15
|
See StarkMessagePaneService clearOnNavigation property Default: |
Public getAll |
Type : Spy<>
|
Default value : createSpy("getAll")
|
Defined in testing/src/message-pane.mock.ts:30
|
See StarkMessagePaneService getAll() method |
Public remove |
Type : Spy<>
|
Default value : createSpy("remove")
|
Defined in testing/src/message-pane.mock.ts:35
|
See StarkMessagePaneService remove() method |
The mock class MockStarkMessagePaneService
can be imported as follows:
import { MockStarkMessagePaneService } from "@nationalbankbelgium/stark-ui/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_MESSAGE_PANE_SERVICE, useValue: new MockStarkMessagePaneService() },
...
]
});
Then you can just inject the Stark service via the TestBed using its corresponding InjectionToken
:
// this will inject the instantiated mock class
mockStarkMessagePaneService = TestBed.inject(STARK_MESSAGE_PANE_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 shouldBeClearedOnNavigation = mockStarkMessagePaneService.clearOnNavigation;
// returning custom value
mockStarkMessagePaneService.getAll.and.returnValue(someCustomObservable);
// overriding a method with a custom function
mockStarkMessagePaneService.add.and.callFake((messages: StarkMessage[]) => {
// some custom logic to add the messages
});
// asserting that a method was indeed called
expect(mockStarkMessagePaneService.add).toHaveBeenCalledTimes(1);
import { StarkMessagePaneService } from "@nationalbankbelgium/stark-ui";
import Spy = jasmine.Spy;
import SpyObj = jasmine.SpyObj;
import createSpy = jasmine.createSpy;
/**
* Mock class of the {@link StarkMessagePaneService} interface.
*/
export class MockStarkMessagePaneService implements SpyObj<StarkMessagePaneService> {
/**
* See [StarkMessagePaneService clearOnNavigation]{@link StarkMessagePaneService#clearOnNavigation} property
*
* Default: `false`.
*/
public clearOnNavigation = false;
/**
* See [StarkMessagePaneService add()]{@link StarkMessagePaneService#add} method
*/
public add: Spy<StarkMessagePaneService["add"]> = createSpy("add");
/**
* See [StarkMessagePaneService addOne()]{@link StarkMessagePaneService#addOne} method
*/
public addOne: Spy<StarkMessagePaneService["addOne"]> = createSpy("addOne");
/**
* See [StarkMessagePaneService getAll()]{@link StarkMessagePaneService#getAll} method
*/
public getAll: Spy<StarkMessagePaneService["getAll"]> = createSpy("getAll");
/**
* See [StarkMessagePaneService remove()]{@link StarkMessagePaneService#remove} method
*/
public remove: Spy<StarkMessagePaneService["remove"]> = createSpy("remove");
/**
* See [StarkMessagePaneService clearAll()]{@link StarkMessagePaneService#clearAll} method
*/
public clearAll: Spy<StarkMessagePaneService["clearAll"]> = createSpy("clearAll");
}