testing/src/http.mock.ts
Mock class of the StarkHttpService interface.
SpyObj
Properties |
|
Public executeCollectionRequest |
Type : Spy<>
|
Default value : createSpy("executeCollectionRequest")
|
Defined in testing/src/http.mock.ts:27
|
See StarkHttpService executeCollectionRequest() method |
Public executeSingleItemRequest |
Type : Spy<>
|
Default value : createSpy("executeSingleItemRequest")
|
Defined in testing/src/http.mock.ts:22
|
See StarkHttpService executeSingleItemRequest() method |
Public Readonly rawHttpClient |
Type : SpyObj<>
|
Default value : createSpyObj<StarkHttpService<T>["rawHttpClient"]>(
"rawHttpClient",
["request", "delete", "get", "head", "jsonp", "options", "patch", "post", "put"]
)
|
Defined in testing/src/http.mock.ts:14
|
See StarkHttpService rawHttpClient property |
The mock class MockStarkHttpService
can be imported as follows:
import { MockStarkHttpService } 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_HTTP_SERVICE, useValue: new MockStarkHttpService() },
...
]
});
Then you can just inject the Stark service via the TestBed using its corresponding InjectionToken
:
// this will inject the instantiated mock class
mockHttpService = TestBed.inject(STARK_HTTP_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
mockHttpService.executeSingleItemRequest.and.returnValue(of(someSingleItemResponseWrapper));
// overriding a method with a custom function
mockHttpService.executeSingleItemRequest.and.callFake((someRequest) => {
// some custom logic to return a specific value
});
// asserting that a method was indeed called
expect(mockHttpService.executeSingleItemRequest).toHaveBeenCalledTimes(1);
expect(mockHttpService.executeSingleItemRequest).toHaveBeenCalledWith(someRequest);
import { StarkHttpService, StarkResource } from "@nationalbankbelgium/stark-core";
import Spy = jasmine.Spy;
import SpyObj = jasmine.SpyObj;
import createSpy = jasmine.createSpy;
import createSpyObj = jasmine.createSpyObj;
/**
* Mock class of the {@link StarkHttpService} interface.
*/
export class MockStarkHttpService<T extends StarkResource> implements SpyObj<StarkHttpService<T>> {
/**
* See [StarkHttpService rawHttpClient]{@link StarkHttpService#rawHttpClient} property
*/
public readonly rawHttpClient: SpyObj<StarkHttpService<T>["rawHttpClient"]> = createSpyObj<StarkHttpService<T>["rawHttpClient"]>(
"rawHttpClient",
["request", "delete", "get", "head", "jsonp", "options", "patch", "post", "put"]
);
/**
* See [StarkHttpService executeSingleItemRequest()]{@link StarkHttpService#executeSingleItemRequest} method
*/
public executeSingleItemRequest: Spy<StarkHttpService<T>["executeSingleItemRequest"]> = createSpy("executeSingleItemRequest");
/**
* See [StarkHttpService executeCollectionRequest()]{@link StarkHttpService#executeCollectionRequest} method
*/
public executeCollectionRequest: Spy<StarkHttpService<T>["executeCollectionRequest"]> = createSpy("executeCollectionRequest");
}