Last active
November 22, 2020 15:51
-
-
Save Markus-Ende/3e9ad7a55c6beea034ed828b802a2497 to your computer and use it in GitHub Desktop.
Mocking Angular Providers with ts-mockito
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mockProvider = <T>( | |
toMock: Type<T>, | |
setupMock: (m: T) => void | |
): { | |
provide: Type<T>; | |
useFactory: () => T; | |
} => { | |
const m = mock(toMock); | |
setupMock(m); | |
return { provide: toMock, useFactory: () => instance(m) }; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TestBed.configureTestingModule({ | |
providers: [ | |
mockProvider(MyService, m => | |
when(m.someFunction()).thenReturn('some value') | |
) | |
] | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment