testing/src/xsrf.mock.ts
Mock class of the StarkXSRFService interface.
SpyObj
Properties |
|
Public configureHttpRequest |
Type : Spy<>
|
Default value : createSpy("configureHttpRequest")
|
Defined in testing/src/xsrf.mock.ts:13
|
See StarkXSRFService configureHttpRequest() method |
Public configureXHR |
Type : Spy<>
|
Default value : createSpy("configureXHR")
|
Defined in testing/src/xsrf.mock.ts:18
|
See StarkXSRFService configureXHR() method |
Public getXSRFToken |
Type : Spy<>
|
Default value : createSpy("getXSRFToken")
|
Defined in testing/src/xsrf.mock.ts:23
|
See StarkXSRFService getXSRFToken( method |
Public pingBackends |
Type : Spy<>
|
Default value : createSpy("pingBackends")
|
Defined in testing/src/xsrf.mock.ts:28
|
See StarkXSRFService pingBackends() method |
Public storeXSRFToken |
Type : Spy<>
|
Default value : createSpy("storeXSRFToken")
|
Defined in testing/src/xsrf.mock.ts:33
|
See StarkXSRFService storeXSRFToken() method |
The mock class MockStarkXsrfService
can be imported as follows:
import { MockStarkXsrfService } 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_XSRF_SERVICE, useValue: new MockStarkXsrfService() },
...
]
});
Then you can just inject the Stark service via the TestBed using its corresponding InjectionToken
:
// this will inject the instantiated mock class
mockXSRFService = TestBed.inject(STARK_XSRF_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 :// returning custom value
mockXSRFService.getXSRFToken.and.returnValue("some token");
// overriding a method with a custom function
mockXSRFService.getXSRFToken.and.callFake(() => {
// some custom logic to return a specific value
});
// asserting that a method was indeed called
expect(mockXSRFService.configureHttpRequest).toHaveBeenCalledTimes(1);
expect(mockXSRFService.configureHttpRequest).toHaveBeenCalledWith(someRequest);
import { StarkXSRFService } from "@nationalbankbelgium/stark-core";
import Spy = jasmine.Spy;
import SpyObj = jasmine.SpyObj;
import createSpy = jasmine.createSpy;
/**
* Mock class of the {@link StarkXSRFService} interface.
*/
export class MockStarkXsrfService implements SpyObj<StarkXSRFService> {
/**
* See [StarkXSRFService configureHttpRequest()]{@link StarkXSRFService#configureHttpRequest} method
*/
public configureHttpRequest: Spy<StarkXSRFService["configureHttpRequest"]> = createSpy("configureHttpRequest");
/**
* See [StarkXSRFService configureXHR()]{@link StarkXSRFService#configureXHR} method
*/
public configureXHR: Spy<StarkXSRFService["configureXHR"]> = createSpy("configureXHR");
/**
* See [StarkXSRFService getXSRFToken(]{@link StarkXSRFService#getXSRFToken} method
*/
public getXSRFToken: Spy<StarkXSRFService["getXSRFToken"]> = createSpy("getXSRFToken");
/**
* See [StarkXSRFService pingBackends()]{@link StarkXSRFService#pingBackends} method
*/
public pingBackends: Spy<StarkXSRFService["pingBackends"]> = createSpy("pingBackends");
/**
* See [StarkXSRFService storeXSRFToken()]{@link StarkXSRFService#storeXSRFToken} method
*/
public storeXSRFToken: Spy<StarkXSRFService["storeXSRFToken"]> = createSpy("storeXSRFToken");
}