Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.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
Unit testing 如何在AngularJS的指令单元测试中注入服务_Unit Testing_Angularjs_Jasmine_Angularjs Directive - Fatal编程技术网

Unit testing 如何在AngularJS的指令单元测试中注入服务

Unit testing 如何在AngularJS的指令单元测试中注入服务,unit-testing,angularjs,jasmine,angularjs-directive,Unit Testing,Angularjs,Jasmine,Angularjs Directive,我需要测试一个指令,该指令对一些注入的服务进行一些调用。 下面的代码是一个示例指令,它侦听事件,并在指定元素内按enter键时重定向浏览器 编辑:我感觉我可能在E2E测试区涉水 问题是,我还没有弄明白如何注入服务来在指令中监视它们 我建议的解决方案如下所示: 它没有像预期的那样工作,因为我没有成功地注入$locacion服务来监视 要装饰、存根、提供模拟或覆盖任何给定服务,您可以使用$provide服务。 $provide.value,$provide.decorator等文档 然后你可以做这样

我需要测试一个指令,该指令对一些注入的服务进行一些调用。 下面的代码是一个示例指令,它侦听事件,并在指定元素内按enter键时重定向浏览器

编辑:我感觉我可能在E2E测试区涉水

问题是,我还没有弄明白如何注入服务来在指令中监视它们

我建议的解决方案如下所示: 它没有像预期的那样工作,因为我没有成功地注入
$locacion
服务来监视


要装饰、存根、提供模拟或覆盖任何给定服务,您可以使用
$provide
服务。
$provide.value
$provide.decorator
等文档

然后你可以做这样的事情:

 var $location;

 beforeEach(function() {
    module('studentportalenApp', function($provide) {
      $provide.decorator('$location', function($delegate) {

        $delegate.path = jasmine.createSpy();

        return $delegate;
      });
    });

    inject(function(_$location_) {
      $location = _$location_;
    });

  });

it('按下enter键时应访问scope.redirectUrl中的链接'),inject(函数($rootScope,$compile,$location){
元素=角度。元素(“”);
element=$compile(element)($rootScope);
$rootScope.redirectUrl='0http://www.google.com';
$rootScope.$digest();
var e=jQuery.Event('keypress');
e、 keyCode=13;
元素。触发器(e);
$rootScope.$digest();
期望($location.path).已通过($location.path)调用http://www.google.com');
}));

在尝试导航之前,您需要创建间谍。我相信,如果您将对“spyOn”的调用移动到函数顶部,它将按预期工作。文档的url已更改:
describe('Directive: gotoOnEnter', function () {
  beforeEach(module('fooApp'));

  var element;

  it('should visit the link in scope.url when enter is pressed', inject(function ($rootScope, $compile, $location) {

    element = angular.element('<input type="text" goto-on-enter>');
    element = $compile(element)($rootScope);

    $rootScope.redirectUrl = 'http://www.google.com';
    $rootScope.$digest();

    var e = jQuery.Event('keypress');
    e.keyCode = 13;
    element.trigger(e);

    spyOn($location, 'path');

    expect($location.path).toHaveBeenCalledWith('http://www.google.com');
  }));
Expected spy path to have been called with [ 'http://www.google.com' ] but it was never called.
 var $location;

 beforeEach(function() {
    module('studentportalenApp', function($provide) {
      $provide.decorator('$location', function($delegate) {

        $delegate.path = jasmine.createSpy();

        return $delegate;
      });
    });

    inject(function(_$location_) {
      $location = _$location_;
    });

  });
it('should visit the link in scope.redirectUrl when enter is pressed', inject(function ($rootScope, $compile, $location) {
    element = angular.element('<input type="text" goto-on-enter>');
    element = $compile(element)($rootScope);

    $rootScope.redirectUrl = 'http://www.google.com';
    $rootScope.$digest();

    var e = jQuery.Event('keypress');
    e.keyCode = 13;
    element.trigger(e);

    $rootScope.$digest();

    expect($location.path).toHaveBeenCalledWith('http://www.google.com');

}));