how to test service property call

262 Views Asked by At

Here is my temp.js

angular.module('temp', [])
    .service('tempFactory', function() {
        this.a = 10;
    })
    .controller('tempCtrl', function($scope, tempFactory) {
        $scope.a = tempFactory.a;
    });

and here is my temp.Spec.js

describe('temp', function() {
    var ctrl, scope;

    beforeEach(function() {
        module('temp');

        inject(function($rootScope, $controller) {
            scope = $rootScope.new();
            ctrl = $controller('tempCtrl', {$scope: scope});
        });
    });
});

I know that to test service method call it is necessary to use spy. But how to test service property call(in my code it is $scope.a = tempFactory.a;)? How can I find out that some property of any service were called?

1

There are 1 best solutions below

5
On

Write a spec that tests the value on scope. No spy needed. Write something to the effect of:

it('sets a to the expected value from the service', function () {
  expect(scope.a).toBe(10);
});