The trouble comes when creating the provider for the mock dependency, as it
needs to use $q
Below is a factory, and we need to know when MyDependency.doAction is
called, and to be able to control the promise.
angular.module('myApp').factory('MyFactory', function (MyDependency, $q) {
return {
doSomething: function () {
var deferred = $q.defer();
MyDependency.doAction().then(function (response) {
deferred.resolve(response);
// other events
}, function (error) {
deferred.reject(error);
// other events
});
return deferred.promise;
}
}
});
Below is the test case, where we attempt to create the MyDependencyProvider
in order to be injectable into the MyFactory service.
describe('Service: MyFactory', function () {
var myDependency, myFactory;
beforeEach(module('myApp'));
beforeEach(module(function ($provide, $q) { // $q can't be loaded until
after providers..
var promise = $q.defer().promise;
myDependency = jasmine.createSpyObj('MyDependency', ['open']);
myDependency.open.andReturn(promise);
$provide.value('MyDependency', {
doAction: myDependency.open
});
}));
beforeEach(inject(function (MyFactory) {
myFactory = MyFactory;
}));
describe('MyDependency.doAction should be called', function () {
myFactory.doSomething();
expect(myDependency.open).toHaveBeenCalled();
// expect other events
});
});
However, the trouble arises when trying to create the promise to be
returned by the mock dependency, since we can't access $q during this stage.
Any ideas on how this can be achievable?
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.