Skip to content

How to mock an interface with ngMocks? #3126

Answered by satanTime
vglaeser asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @vglaeser,

ng-mocks helps to mock classes and to create a mock instance of their type. However, interfaces are a different topic and ng-mocks doesn't have anything to help here.

If both ApiService and ApiServiceImpl1 are classes, then it's possible to do with double mocking:

  • mock ApiService
  • expand the mock with a mock of ApiServiceImpl1
@NgModule({
  providers: [
    MockProvider(ApiService, MockService(ApiServiceImpl1, {
      // ... overrides here if needed
    })),
  ],
})
class TargetModule {}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@vglaeser
Comment options

Answer selected by vglaeser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants