File

testing/src/authorization.mock.ts

Description

Mock class of the StarkRBACAuthorizationService interface.

Implements

SpyObj

Index

Properties

Properties

Public hasAnyRole
Type : Spy<>
Default value : createSpy("hasAnyRole")
Public hasRole
Type : Spy<>
Default value : createSpy("hasRole")
Public initializeService
Type : Spy<>
Default value : createSpy("initializeService")
Public isAnonymous
Type : Spy<>
Default value : createSpy("isAnonymous")

Usage

The mock class MockStarkRBACAuthorizationService can be imported as follows:

Example :
import { MockStarkRBACAuthorizationService } from "@nationalbankbelgium/stark-rbac/testing";

Since the mock class implements the base interface of the service it mocks, you just need to provide the mock in your TestingModule:

Example :
TestBed.configureTestingModule({
    imports: [...],
    declarations: [...],
    providers: [
        ...
        { provide: STARK_RBAC_AUTHORIZATION_SERVICE, useValue: new MockStarkRBACAuthorizationService() },
        ...
    ]
});

Then you can just inject the Stark service via the TestBed using its corresponding InjectionToken:

Example :
// this will inject the instantiated mock class
mockRBACAuthorizationService = TetBed.get(STARK_RBAC_AUTHORIZATION_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:

Example :
// returning custom value
mockRBACAuthorizationService.hasRole.and.returnValue(true);

// overriding a method with a custom function
mockRBACAuthorizationService.hasRole.and.callFake((someRole) => {
  // some custom logic to return a specific value
});

// asserting that a method was indeed called
expect(mockRBACAuthorizationService.hasAnyRole).toHaveBeenCalledTimes(1);
expect(mockRBACAuthorizationService.hasAnyRole).toHaveBeenCalledWith(someRoles);

import { StarkRBACAuthorizationService } from "@nationalbankbelgium/stark-rbac";
import Spy = jasmine.Spy;
import SpyObj = jasmine.SpyObj;
import createSpy = jasmine.createSpy;

/**
 * Mock class of the {@link StarkRBACAuthorizationService} interface.
 */
export class MockStarkRBACAuthorizationService implements SpyObj<StarkRBACAuthorizationService> {
	/**
	 * See [StarkRBACAuthorizationService initializeService()]{@link StarkRBACAuthorizationService#initializeService} method
	 */
	public initializeService: Spy<StarkRBACAuthorizationService["initializeService"]> = createSpy("initializeService");

	/**
	 * See [StarkRBACAuthorizationService hasRole()]{@link StarkRBACAuthorizationService#hasRole} method
	 */
	public hasRole: Spy<StarkRBACAuthorizationService["hasRole"]> = createSpy("hasRole");

	/**
	 * See [StarkRBACAuthorizationService hasAnyRole()]{@link StarkRBACAuthorizationService#hasAnyRole} method
	 */
	public hasAnyRole: Spy<StarkRBACAuthorizationService["hasAnyRole"]> = createSpy("hasAnyRole");

	/**
	 * See [StarkRBACAuthorizationService isAnonymous()]{@link StarkRBACAuthorizationService#isAnonymous} method
	 */
	public isAnonymous: Spy<StarkRBACAuthorizationService["isAnonymous"]> = createSpy("isAnonymous");
}

results matching ""

    No results matching ""