File

testing/src/http.mock.ts

Description

Mock class of the StarkHttpService interface.

Implements

SpyObj

Index

Properties

Properties

Public executeCollectionRequest
Type : Spy<>
Default value : createSpy("executeCollectionRequest")
Public executeSingleItemRequest
Type : Spy<>
Default value : createSpy("executeSingleItemRequest")
Public Readonly rawHttpClient
Type : SpyObj<>
Default value : createSpyObj<StarkHttpService<T>["rawHttpClient"]>( "rawHttpClient", ["request", "delete", "get", "head", "jsonp", "options", "patch", "post", "put"] )

Usage

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 = TetBed.get(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:

  • 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:

// 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");
}

result-matching ""

    No results matching ""