如何使用额外参数测试指令?

时间:2013-08-29 11:09:32

标签: angularjs angularjs-directive jasmine

我正在尝试使用注入的常量(即SEARCH_URL)来测试指令:

.directive('autocomplete', [
  "$rootScope",
  "SEARCH_URL",
  function (
    $rootScope,
    SEARCH_URL
  ) {
  // ...
});

进行此测试:

describe('autocomplete directive', function () {
  "use strict";

  var element, scope;

  beforeEach(module('nav'));

  beforeEach(function() {
    inject(function($compile, $rootScope) {
      var linkingFn = $compile("<div ng-model='search' autocomplete></div>");
      scope = $rootScope;
      element = linkingFn(scope);
    });
  });

  it('selection resets autocomplete matches', function() {
    scope.select({ Title: "Something" });
    expect(scope.autocompleteMatches.length).toEqual(0);
  });
});

但是,我不确定在测试中注入常量的位置。我运行测试时出现此错误

PhantomJS 1.9 (Mac) autocomplete directive selection resets autocomplete matches FAILED
    Error: Unknown provider: SEARCH_URLProvider <- SEARCH_URL <- autocompleteDirective
        at /Users/ttt/Work/Dev/az/az-search-html/app/public/vendor/angular-unstable/angular.min.js:29

在另一个控制器测试中,我可以用

之类的东西注入额外的参数
$controller('ArticleCtrl', params);

我如何为指令做到这一点?

1 个答案:

答案 0 :(得分:2)

您可以使用angular.mocks来存储常量,如下所示:

  beforeEach(function() {
    module(function($provide) {
      $provide.constant('SEARCH_URL', 'my seach url');
    });
  });