shanahobo86
shanahobo86

Reputation: 497

Configure Angular service for use in AngularJS module unit tests

I'm using an Angular service in an AngularJS module downgraded using downgradeInjectible. The app is working fine with the downgraded service but all my unit tests are failing with the below error:

Error: Error while instantiating injectable 'MyAngularService': Not a valid '@angular/upgrade' application. Did you forget to downgrade an Angular module or include it in the AngularJS application?

Do I need to use downgradeInjectable again in my karma test setup to configure this service for unit tests?

I was testing the below code to downgrade the service in test setup but I don't think it's the right approach.

// downgrade service for unit tests
beforeEach(angular.mock.module('myModule').factory('MyAngularService',downgradeInjectable(MyAngularService) as any))

Any advice on how to resolve this issue?

Upvotes: 2

Views: 1033

Answers (1)

childersd
childersd

Reputation: 159

Bit late to the party, but I just figured this out. You can do

angular.mock.module(($provide: ng.auto.IProvideService) => {
  $provide.value("MyAngularService", myAngularServiceMock));
});

as part of the beforeEach in your tests. This way you'd provide a previously created mock instance of myAngularServiceMock as the expected MyAngularService.

Upvotes: 2

Related Questions