我真的希望这是一个微不足道的问题。是的,我有RTFM。我实际上使用Angular记录的方式来注入控制器,但由于某种原因我的控制器没有定义。这里的主要区别是我习惯于单模块应用程序,这次我有一个多模块应用程序。我不认为它会有所作为,但你去了。而不是冗长的描述,我会直接看到代码:
使用Angular 1.2.16
单元测试框架:Jasmine
app.js
angular.module('OBB', [
// Native AngularJS DI
'ngResource', 'ngCookies',
// bunch of modules
...
// OBB Page Modules
'OBB.home', 'OBB.buckets', 'OBB.company', 'OBB.advSearch', 'OBB.users'
])
所以,我正在尝试在OBB.home模块中测试一个控制器。
home.js
angular.module('OBB.home', ['ui.router'])
.controller('HomeCtrl', ['$log', '$rootScope', '$scope', '$state', 'AUTH_EVENTS',
function HomeCtrl ($log, $rootScope, $scope, $state, AUTH_EVENTS) {
$scope.signInFormData = {
email: null, password: null
};
//more code...
}]);
home.spec.js
describe('Unit Home Controllers: ', function () {
var homeController, scope;
beforeEach(module('OBB.home'));
beforeEach(inject(function (_$rootScope_, $controller) {
scope = _$rootScope_.$new();
homeController = $controller('HomeCtrl', {
$rootScope: _$rootScope_,
$scope: scope,
$log: {},
$state: {},
AUTH_EVENTS: {},
});
}));
it('Home Controller is correctly instantiated', inject(function () {
expect(scope).toBeDefined(); // Pass
expect(scope.signInFormData).toBeDefined(); // Fails
}));
});
答案 0 :(得分:0)
您需要模拟/加载依赖项。
describe('Unit Home Controllers: ', function () {
var homeController, scope;
beforeEach(module('OBB.home'));
beforeEach(inject(function (_$rootScope_, $controller) {
scope = _$rootScope_.$new();
homeController = $controller('HomeCtrl', {
$rootScope: _$rootScope_,
$scope: scope,
$log: {}, //You will have to add methods as needed
$state: {},
AUTH_EVENTS: {}
});
}));
it('Home Controller is correctly instantiated', inject(function () {
expect(scope).toBeDefined(); // Fails
expect(scope.signInFormData).toBeDefined(); // Fails
}));
});