-
Hey, is there a way to mock an interface using ngMocks? For example, I use an interface which declares some methods to communicate with a server. Depending on the use-case, different classes (implementing the interface) are injected, like:
When I use
the created mock will be empty, i.e. there are no mocks for methods or fields declared by the interface. I found the following workaround but it's rather inconvenient:
However, this way I have to reference classes in my test that are not required by the tested service/component which typically only knows the interface. Is there an easier way of mocking of an interface? Thanks in advance |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @vglaeser,
If both
@NgModule({
providers: [
MockProvider(ApiService, MockService(ApiServiceImpl1, {
// ... overrides here if needed
})),
],
})
class TargetModule {} |
Beta Was this translation helpful? Give feedback.
Hi @vglaeser,
ng-mocks
helps to mock classes and to create a mock instance of their type. However, interfaces are a different topic andng-mocks
doesn't have anything to help here.If both
ApiService
andApiServiceImpl1
are classes, then it's possible to do with double mocking:ApiService
ApiServiceImpl1