How to test with Jasmine an AngularJS controller that calls service method that returns promise

后端 未结 1 507
[愿得一人]
[愿得一人] 2020-12-24 08:54

I am using v1.2.0-rc.3 of AngularJS with Jasmine test framework.

I am trying to assert that a controller calls a service method. The service method returns a promise

1条回答
  •  南方客
    南方客 (楼主)
    2020-12-24 09:18

    It seems I was placing my spy in the wrong place. When I place it in the beforeEach, the test passes.

      beforeEach(inject(function($controller, $rootScope, $q) {
        svc = {
          query: function () {
            def = $q.defer();
            return def.promise;
          }
        };
        spyOn(svc, 'query').andCallThrough();
        scope = $rootScope.$new();
        controller = $controller('ctrl', {
          $scope: scope,
          svc: svc
        });
      }));
    

    0 讨论(0)
提交回复
热议问题