1

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);

我如何为指令执行此操作?

4

1 回答 1

2

您可以使用 angular.mocks 像这样对常量进行存根:

  beforeEach(function() {
    module(function($provide) {
      $provide.constant('SEARCH_URL', 'my seach url');
    });
  });
于 2013-08-30T03:18:31.593 回答