I've encountered an issue while trying to test my self-written service that utilizes $resource. I'm facing difficulties when trying to inject $resource into my test spec.
My setup includes Typescript with AngularJS 1.6.x, and here is a snippet of my service class:
export class MyDataService {
constructor(
private $resource: ng.resource.IResourceService
) {
this.someResource = $resource('api/some-endpoint');
}
public getThings() {
return this.someResource.get().$promise;
}
}
Below is my test spec using Jasmine + Karma:
import { MyDataService } from './my-data-service.service';
describe('MyDataService Tests:', function() {
let myDataService: MyDataService;
let $resource: ng.resource.IResourceService;
let resourceSpy: jasmine.Spy;
beforeEach(inject(function(_$resource_){
$resource = _$resource_;
myDataService = new MyDataService($resource);
resourceSpy = spyOn(myDataService.resource, 'get');
}));
afterEach(function() {
resourceSpy.calls.reset();
});
describe('when calling getThings', function() {
it('should call get on the underlying resource instance', function() {
myDataService.getThings(1, 15);
expect(resourceSpy).toHaveBeenCalled();
});
});
The error message
Unknown provider: $resourceProvider <- $resource
keeps popping up, even though angular-resource is correctly included in my Karma config.
I'm also using angular-mocks with Karma. Could the reason for this error be that $resource isn't part of the mocks library? I haven't specified any angular modules to load in my tests as I aim to test classes in isolation without loading an entire module.
Thank you.