Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 带有ui路由器$state.current的测试指令_Angularjs_Jasmine_Karma Jasmine_Angular Directive - Fatal编程技术网

Angularjs 带有ui路由器$state.current的测试指令

Angularjs 带有ui路由器$state.current的测试指令,angularjs,jasmine,karma-jasmine,angular-directive,Angularjs,Jasmine,Karma Jasmine,Angular Directive,试图测试指令,但我不知所措。基本上,我不知道如何设置测试,也找不到任何示例一步一步地告诉我。有人能解释一下该如何设置吗?现在我得到的错误是 TypeError:“undefined”不是对象(正在计算“current.name”) directives.directive('convenienceNav',function(){ 返回{ 限制:“A”, 模板:“添加{{$state.current.params.singular_title}” }; }); description('dire

试图测试指令,但我不知所措。基本上,我不知道如何设置测试,也找不到任何示例一步一步地告诉我。有人能解释一下该如何设置吗?现在我得到的错误是

TypeError:“undefined”不是对象(正在计算“current.name”)

directives.directive('convenienceNav',function(){
返回{
限制:“A”,
模板:“添加{{$state.current.params.singular_title}”
};
}); 
description('directive:convenienceNav',function(){
变量元素、范围、状态参数、状态;
在每个模块之前(模块(“应用程序指令”);
在每个(模块('ui.router')之前;
beforeach(注入函数($rootScope、$compile、$state){
scope=$rootScope.$new();
stateParams={'api_resource_name':'people'};
state=$state;
state.current.name='admin.people';
//state.current.params.singular_title='Person';
元素=“”;
元素=$compile(元素)(范围);
范围。$digest();
}));
它('应该有state.current.name=admin.people',函数(){
expect(element.html()).toBe('addperson');
});
});

以下是如何在jasmine中测试角度指令和路线导航(ui路由器)的示例

带有名称的按钮

var namedButtonModule=angular.module('namedButtonModule',[]);
namedButtonModule.directive('namedButton',function(){
返回{
限制:“AE”,
范围:{
名称:“=?”
},
模板:“我是{{name}}按钮”,
控制器:功能($scope){
$scope.name=$scope.name | |‘简单’;
}
};
});
description('指令:namedButton',函数(){
在每个(模块('namedButtonModule')之前;
beforeach(注入(函数($rootScope,$compile){
这。$rootScope=$rootScope;
这个。$compile=$compile;
this.testContainer=document.getElementById('test-container');
this.compileDirective=函数(模板,范围){
var元素=这个$compile(模板)(范围);
this.testContainer.appendChild(元素[0]);
范围。$digest();
返回元素;
}
}));
之后(函数(){
this.testContainer.innerHTML='';
});
它('按钮应显示默认名称',函数(){
var模板=“”;
var scope=this.$rootScope.$new();
var元素=this.compileDirective(模板,范围);
expect(element.text()).toBe('I am simple Button');
});
它('按钮应显示传递给作用域的名称',函数(){
var模板=“”;
var scope=this.$rootScope.$new();
scope.inputName=“角度测试”;
var元素=this.compileDirective(模板,范围);
expect(element.text()).toBe('我是角度测试按钮');
});
});

directives.directive('convenienceNav', function(){
  return {
    restrict: 'A',
    template: '<button class="btn btn-success" ui-sref="  {{$state.current.name}}.add"><i class="fa fa-user-plus"></i>Add   {{$state.current.params.singular_title}}</button>'
    };

}); 



describe('directive: convenienceNav', function() { 
  var element, scope, stateParams, state;

  beforeEach(module('app.directives'));
  beforeEach(module('ui.router'));


  beforeEach(inject(function($rootScope, $compile, $state) {
    scope = $rootScope.$new();
    stateParams = {'api_resource_name': 'people'};
    state = $state;

   state.current.name = 'admin.people';

   // state.current.params.singular_title = 'Person';
    element ='<div convenience-nav></div>';

    element = $compile(element)(scope);
    scope.$digest();
  }));

  it('should have state.current.name = admin.people', function(){
      expect(element.html()).toBe('<button class="btn btn-success" ui-sref="admin.people.add"><i class="fa fa-user-plus"></i>Add Person</button>');

      });
});