1
以下のようなエラーメッセージが表示されます。私は解決策を見つけることができません。 類似のサービスモックが渡されます。しかし、なぜそれがエラーを引き起こすのかわかりません。AngularJS/Jasmine undefinedはコンストラクタではありません
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)
[email protected]/scripts/controllers/appBar.js:19:26
test/spec/controllers/appBar.js:46:21
[email protected]://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サービスモックは機能しません。 コントローラコードで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();
});
});
});
ありがとうございました!できます。しかし、私は1つの質問があります。なぜmdSidenavサービスをコントローラに追加しなくてもmdSidenavが機能するのですか? –
です。なぜなら、スコープが$ scope.toggleSidenavに関連付けられていて、コントローラにスコープを注入しているのに対して、firebaseAuthはコントローラにスコープを注入しているからです。 –