【问题标题】:AngularJS / Jasmine undefined is not a constructorAngularJS / Jasmine undefined 不是构造函数
【发布时间】:2016-09-03 09:09:02
【问题描述】:

我收到如下错误消息。我找不到解决方案。 通过了类似的服务模拟。但我不知道为什么会导致错误。

PhantomJS 2.1.1 (Mac OS X 0.0.0) AppBarCtrl $scope.signOut should call firebaseAuth.$signOut FAILED
TypeError: undefined is not a constructor (evaluating 'firebaseAuth.$signOut()') in app/scripts/controllers/appBar.js (line 19)
signOut@app/scripts/controllers/appBar.js:19:26
test/spec/controllers/appBar.js:46:21
loaded@http://localhost:8080/context.js:151:17

这些是我的控制器和测试代码。

(function() {
'use strict';

angular.module('authApp')
  .controller('AppBarCtrl', function($scope, $location, $mdSidenav, firebaseAuth) {
    $scope.toggleSidenav = function() {
        $mdSidenav('sidenav').toggle();
    };

    $scope.signOut = function() {
        firebaseAuth.$signOut();
        $location.path('/');
    };
  });
})();

mdSidenav 运行良好,但 firebaseAuth 服务模拟不起作用。 在Controller代码中调用firebaseAuth.$signOut();会报错。

describe('AppBarCtrl', function() {
  beforeEach(module('authApp'));

  var mdSidenav, firebaseAuth;

  beforeEach(module(function($provide) {
    mdSidenav = {};
    mdSidenav.toggle = jasmine.createSpy('toggle');
    $provide.factory('$mdSidenav', function() {
      return function(componentId) {
        return mdSidenav;
      };
    });
  }));

  beforeEach(module(function($provide) {
    firebaseAuth = {};
    firebaseAuth.$signOut = jasmine.createSpy('$signOut');
    $provide.factory('firebaseAuth', function() {
      return function() {
        return firebaseAuth;
      };
    });
  }));

  var $controller;

  beforeEach(inject(function(_$controller_) {
    $controller = _$controller_;
  }));

  describe('$scope.toggleSidenav', function() {
    it('should call $mdSidenav.toggle()', function() {
      var $scope = {};
      var controller = $controller('AppBarCtrl', { $scope: $scope });
      $scope.toggleSidenav();
      expect(mdSidenav.toggle).toHaveBeenCalled();
    })
  });

  describe('$scope.signOut', function() {
    it('should call firebaseAuth.$signOut', function() {
      var $scope = {};
      var controller = $controller('AppBarCtrl', { $scope: $scope });
      console.log(firebaseAuth);
      $scope.signOut();
      expect(firebaseAuth.$signOut).toHaveBeenCalled();
    });
  });
});

【问题讨论】:

    标签: angularjs firebase jasmine firebase-authentication


    【解决方案1】:

    您需要先使用控制器的实例添加该服务

    describe('$scope.signOut', function() {
    it('should call firebaseAuth.$signOut', function() {
      var $scope = {};
      var controller = $controller('AppBarCtrl', { $scope: $scope,firebaseAuth:firebaseAuth });
      console.log(firebaseAuth);
      $scope.signOut();
      expect(firebaseAuth.$signOut).toHaveBeenCalled();
    });
    

    });

    【讨论】:

    • 谢谢!有用。但我有 1 个问题。即使我没有将 mdSidenav 服务添加到控制器,为什么 mdSidenav 仍然有效?
    • 因为它与范围相关联,即 $scope.toggleSidenav 并且您正在使用控制器注入范围,而 firebaseAuth 则没有。
    猜你喜欢
    • 2015-09-12
    • 2017-10-08
    • 2016-08-10
    • 1970-01-01
    • 2021-12-13
    • 2014-06-13
    • 2017-08-31
    • 2017-04-27
    • 2017-01-10
    相关资源
    最近更新 更多