angulartestingmockingng-mocks

Difference of mock() vs. provide() in ng-mocks


What is the difference between

https://ng-mocks.sudo.eu/api/MockBuilder#mock

MockBuilder(TargetService, TargetModule)
  .mock(Service2, { trigger: () => 'mock2' })

and

https://ng-mocks.sudo.eu/api/MockBuilder#provide

MockBuilder(TargetService, TargetModule)
  .provide({ provide: Service2, useValue: { trigger: () => 'mock2' }})

Solution

  • .mock will replace the declaration with a mock at its definition, even if it's deeply nested NgModules.

    .provide simply adds the declaration to providers of TestBed and doesn't do any additional replacements in imported NgModules.