I've updated the provider section to create a "fake" promise, but this has 
much less control and usability than deferred.resolve() and 
deferred.reject(); I'll need to create some methods to access a condition 
on whether to call success or fail, which is annoying.
Is there any way to achieve this with $q at the provider level?

beforeEach(module(function ($provide) {
  myDependency = jasmine.createSpyObj('MyDependency', ['doAction']);
  myDependency.doAction.andCallFake(function (){
    return {
      then: function (success, err){
        success.call(this);
      }
    };
  });
  $provide.value('MyDependency', {
    open: myDependency.open
  }); 

}));


On Thursday, July 31, 2014 3:10:23 PM UTC-4, Atticus White wrote:
>
> 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.

Reply via email to