在开始之前,我已按照此处的建议取得了成功:How can I test a controller with resolve properties in AngularJS?
我的app.js,我有一个简单的决心:
var app = angular.module('myApp', ['ui.router']);
app.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('index', {
url: '/index',
templateUrl: 'html/home.html',
controller: 'HomeController',
resolve: {
homeInfo: function() {
return {
subtitle : 'Welcome to tables, ladders and chairs'
};
}
}
})
在我的HomeController.js中:
app.controller('HomeController', function($scope, srv1, srv2, homeInfo, $rootScope) {
$rootScope.$broadcast('brdSubtitle', homeInfo.subtitle);
});
我的测试:
describe('HomeController', function () {
var $rootScope, $scope, $controller, homeController;
beforeEach(module('myApp'));
beforeEach(inject(function (_$rootScope_, _$controller_) {
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
$controller = _$controller_;
homeController = $controller('HomeController', {'$rootScope': $rootScope, '$scope': $scope, homeInfo: { subtitle : 'Welcome to tables, ladders and chairs' }});
}));
it('should exist', function() {
expect(homeController).toBeDefined();
});
it('should have a subtitle', function() {
expect(homeController.homeInfo.subtitle.text()).toBe('Welcome to tables, ladders and chairs');
});
});
错误是:
TypeError: homeController.homeInfo is undefined in /path/to/specs.js
答案 0 :(得分:2)
您应该尝试在beforeEach
中模拟服务值,因为显然将值放在$controller
参数中不起作用。
beforeEach(module(function($provide) {
$provide.value('homeInfo', {
subtitle : 'Welcome to tables, ladders and chairs'
});
}));
答案 1 :(得分:1)
没错,你在控制器中没有this.homeInfo = homeInfo;
,在这种情况下homeController.homeInfo
应该是未定义的。
没有理由为什么应该测试模拟homeInfo
本地依赖项。测试控制器中发生的事情:
beforeEach(inject(function (_$rootScope_, _$controller_) {
...
brdSubtitleListener = jasmine.createSpy('brdSubtitleListener');
$scope.$on('brdSubtitle', brdSubtitleListener);
homeController = $controller('HomeController', {'$rootScope': $rootScope, '$scope': $scope, homeInfo: { subtitle : 'Welcome to tables, ladders and chairs' }});
}));
it('should broadcast a subtitle', function() {
expect(brdSubtitleListener).toHaveBeenCalledWith(jasmine.any(Object), 'Welcome to tables, ladders and chairs');
});