Can I use spyOn by the injectable service method?

I am writing tests for a controller in Jasmine; the controller depends on the service I am inserting with beforeEach(inject). However, when I try to spy on a method in a service, I always get the following error:

spyOn could not find an object to spy upon for replacePod()

The code for my test is as follows:

describe('Controller: ResultsController', function () {
    'use strict';
    var ctrl;
    var ResultsService;
    var RouteService;
    var PodService;
    var $scope;
    var httpMock;
    var log;
    // load the controller module
    beforeEach(module('waApp'));路
    // Initialize the controller and a mock scope
    路路路路
    beforeEach(inject(function(
            $rootScope,
            $log,
            $controller,
            $httpBackend,
            ResultsService,
            RouteService,
            PodService
        ) {
            $scope =  $rootScope.$new();
            log = $log;
            log.reset();
            httpMock = $httpBackend;
            ctrl = $controller('ResultsController', {
                $scope: $scope,
                ResultsService: ResultsService,
                RouteService: RouteService,
                PodService: PodService
            });
    }));

    it('processRecalculate should call PodService.replacePod', function() {
        $scope.getResults({input:'kitten'});
        spyOn(PodService, 'replacePod');
        expect(PodService.replacePod).toHaveBeenCalled();
    });

This gives the above error. Is there something wrong with my code, or is it just impossible to look into the method in the injected service?

+4
source share
2 answers

You do not assign the entered service to a variable outside. So it is undefined when the spy is on it.

The code in the injection should look like this:

beforeEach(inject(function(
  $rootScope,
  $log,
  $controller,
  $httpBackend,
  _ResultsService_,
  _RouteService_,
  _PodService_
) { 
  $scope = $rootScope.$new();
  log = $log;
  log.reset();
  httpMock = $httpBackend;
  ResultsService = _ResultsService_;
  RouteService = _RouteService_;
  PodService = _PodService_;
  ctrl = $controller('ResultsController', {
    $scope: $scope,
    ResultsService: ResultsService,
    RouteService: RouteService,
    PodService: PodService
  });
}));

Hope this helps.

+5
source

, .

jasmine.spec ( coffeescript), .

describe 'AppService', ->

    beforeEach ->
        module 'amorphosite'

    beforeEach inject ($injector) ->
        @rootScope = $injector.get '$rootScope'
        @appService = $injector.get 'appService'
        @userService = $injector.get 'userService'
        @restoreService = $injector.get 'restoreService'
        @transitionService = $injector.get 'transitionService'
        @scope = @rootScope.$new()

    describe 'setRootWatch', ->
        describe 'when $stateChangeStart event is triggered and the user is null', ->
            beforeEach ->
                @appService.setRootWatch @scope

            describe 'and the user cannot be restored', ->
                beforeEach ->
                    spyOn(@userService, 'userIsNull').andReturn true
                    spyOn(@restoreService, 'canRestore').andReturn false
                    spyOn @transitionService, 'gotoLogin'

                it 'it should transition to the login state', ->
                    @scope.$broadcast '$stateChangeStart', {name:'[protected-state]'}
                    expect(@transitionService.gotoLogin).toHaveBeenCalled()

            describe 'and the user can be restored', ->
                beforeEach ->
                    spyOn(@userService, 'userIsNull').andReturn true
                    spyOn(@restoreService, 'canRestore').andReturn true
                    spyOn @restoreService, 'restore'

                it 'it should restore the available data', ->
                    @scope.$broadcast '$stateChangeStart', {name:'[protected-state]'}
                    expect(@restoreService.restore).toHaveBeenCalled()
0

All Articles