Wie man mit Jasmine die anonyme Funktion ausspioniert

Ich benutze Jasmine, um meine eckige Anwendung zu testen und möchte eine anonyme Funktion ausspionieren. Verwenden des Angular-Benachrichtigungsdiensteshttps: //github.com/cgross/angular-notif, Ich möchte wissen, ob die Benachrichtigungsfunktion aufgerufen wurde oder nicht.

Hier ist mein Controller:

angular.module('module').controller('MyCtrl', function($scope, MyService, notify) {

  $scope.isValid = function(obj) {
    if (!MyService.isNameValid(obj.name)) {
      notify({ message:'Name not valid', classes: ['alert'] });
      return false;
    }
  }
});

Und hier ist mein Test:

'use strict';

describe('Test MyCtrl', function () {
  var scope, $location, createController, controller, notify;

  beforeEach(module('module'));

  beforeEach(inject(function ($rootScope, $controller, _$location_, _notify_) {
    $location = _$location_;
    scope = $rootScope.$new();
    notify = _notify_;

    notify = jasmine.createSpy('spy').andReturn('test');

    createController = function() {
      return $controller('MyCtrl', {
        '$scope': scope
      });
    };
  }));

  it('should call notify', function() {
    spyOn(notify);
    controller = createController();
    scope.isValid('name');
    expect(notify).toHaveBeenCalled();
  });
});

ine offensichtliche Rückkehr:

Error: No method name supplied on 'spyOn(notify)'

Denn es sollte so etwas wie spyOn sein (notify, 'method'), aber da es eine anonyme Funktion ist, hat es keine Methode.

Danke für Ihre Hilfe

Antworten auf die Frage(4)

Ihre Antwort auf die Frage