I am currently working with an angular factory that looks like this:
.factory('widgetFactory', ['$http', function($http){
function getWidgets(){
return $http.get('http://example.com/api/widgets/')
.then(function(response){
return response;
});
}
return {
getWidgets:getWidgets
};
}])
Additionally, I have created a jasmine test for the widgetFactory:
describe('widgetFactory', function ($q) {
var mockHttp,
fakeResponse = 'response'
beforeEach(function() {
mockHttp = {
get: jasmine.createSpy('get spy').and.callFake(function () {
var deferred = $q.defer();
deferred.resolve(fakeResponse);
return deferred.promise;
})
};
module(function ($provide) {
$provide.value('$http', mockHttp);
});
});
it('should properly call the API when getWidgets method is invoked', inject(function (widgetFactory) {
var result;
widgetFactory.getWidgets().then(function(response){
result = response;
});
expect(mockHttp.post).toHaveBeenCalledWith('http://example.com/api/widgets/');
expect(result).toBe(fakeResponse);
}));
});
However, during testing, I encountered the following error message: "describe does not expect a done parameter"
After some investigation, I suspect the issue may be related to how I'm utilizing $q in my test. Other examples I've come across show the usage of inject(function($q){ ...
inside the beforeEach
block. Unfortunately, due to my use of module
within the beforeEach
, I end up facing another error: "Injector already created, can not register a module!". Any suggestions or insights on how to resolve this?