testing/src/authorization.mock.ts
Mock class of the StarkRBACAuthorizationService interface.
SpyObj
Properties |
|
Public hasAnyRole |
Type : Spy<>
|
Default value : createSpy("hasAnyRole")
|
Defined in testing/src/authorization.mock.ts:23
|
See StarkRBACAuthorizationService hasAnyRole() method |
Public hasRole |
Type : Spy<>
|
Default value : createSpy("hasRole")
|
Defined in testing/src/authorization.mock.ts:18
|
See StarkRBACAuthorizationService hasRole() method |
Public initializeService |
Type : Spy<>
|
Default value : createSpy("initializeService")
|
Defined in testing/src/authorization.mock.ts:13
|
Public isAnonymous |
Type : Spy<>
|
Default value : createSpy("isAnonymous")
|
Defined in testing/src/authorization.mock.ts:28
|
See StarkRBACAuthorizationService isAnonymous() method |
The mock class MockStarkRBACAuthorizationService
can be imported as follows:
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
:
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
:
// 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:
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");
}