我正在尝试测试我的路由器功能。我收到此错误 无法读取未定义的属性'href' ..请你告诉我如何删除这个错误
这是我的代码 http://plnkr.co/edit/mZiNteMWqS4forycMHiK?p=preview
describe('router check', function(){
describe('router check',function(){
var view = 'partials/state1.html',
$state;
var $scope;
beforeEach(function(){
module('app');
});
beforeEach(inject(function($rootScope,$templateCache,$state) {
$state=$state;
$templateCache.put(view, '');
}));
it('should map state state1 to url /state1 ', function() {
expect($state.href('state1', {})).to.equal('/state1');
});
it('should map /state1 route to state1 View template', function () {
expect($state.get('state1').templateUrl).to.equal(view);
});
})
答案 0 :(得分:2)
您的代码几乎没有问题。我已修复它们并按预期工作。
链接到plunkr
describe('router check', function(){
describe('router check',function(){
var view = 'partials/state1.html',
$state, $templateCache, $scope;
beforeEach(function(){
module('app');
inject(function(_$state_, _$templateCache_) {
$state = _$state_;
$templateCache = _$templateCache_;
});
$templateCache.put(view, '');
});
it('should map state state1 to url /state1 ', function() {
expect($state.href('state1', {})).toEqual('#/state1');
});
it('should map /state1 route to state1 View template', function () {
expect($state.get('state1').views.testView.templateUrl).toEqual(view);
});
});
});
答案 1 :(得分:0)
问题是您没有在正确的范围内访问$ state。 beforeEach中的$ state = $ state语句不会将状态分配给describe中声明的变量“$ state”。
尝试
describe('router check',function(){
var view = 'partials/state1.html',
$state_test;
var $scope;
beforeEach(function(){
module('app');
});
beforeEach(inject(function($rootScope,$templateCache,$state) {
$state_test=$state;
$templateCache.put(view, '');
}));