主要模块注入了所有内容;
require('./dashboard');
module.exports = angular.module('college', ['college.dashboard'])
.config(function ($stateProvider) {
$stateProvider
.state('college.list', {
url: '/college',
templateUrl: '/dashboard/dashboard.html',
controller: 'DashboardCtrl',
authenticate: true
});
})
.factory('ProjectFactory', require('./services/college.service'));
College Index,它使仪表板控制器可用;
module.exports = angular.module('college.dashboard',
[])
.controller('DashboardCtrl', require('./dashboard.controller.js'));
大学管理者公开以下方法;
module.exports = function($scope, $rootScope, $state) {
$scope.openCollege = function(id) {
$rootScope.currentCollege = id;
$state.go('college.main', {currentCollege: id});
};
};
单元测试调用
时抛出以下错误scope.openCollege (2);
错误:
Error: Could not resolve 'college.main' from state ''
创建状态;
beforeEach(inject(function ($rootScope, $state, $location, $controller) {
scope = $rootScope.$new();
location = $location;
rootScope = $rootScope;
$rootScope.currentCollege = {};// Empty by default
state = $state;
$controller('DashboardCtrl', {
$scope: scope,
$state: state,
$location: location
});
}));
一些规范测试代码;
expect(state.current.name).to.equal('');
scope.openCollege(2);
我需要弄清楚如何在Karma单元测试期间处理/模拟 $ state.go ,以便状态了解college.main。
感谢任何帮助。
Ĵ
答案 0 :(得分:4)
以下是我如何运作;
我在规范测试中添加了以下内容;
// Globally defined
var stateSpy;
// within the beforeEach
stateSpy = sinon.stub($state, 'go');
// In the unit test
scope.openCollege (2);
assert(stateSpy.withArgs('college.main', '{currentCollege: 2}').calledOnce);
注意: $ state 未传递给控制器。
我现在有绿色测试!
感谢您的帮助,让我知道如何使这项工作。
Ĵ
答案 1 :(得分:2)
你应该使用
it('should be able to go to person edit state', function () {
DashboardCtrl();
scope.openProject('12345');
scope.$digest();
expect(state.go).toHaveBeenCalledWith('college.main', { id : '12345' });
});