0

在这个测试中,我不确定为什么需要在这两行中将 angular 变量设置为注入参数。是不是因为注入没有自动分配 $compile 和 $rootScope?

 $compile = $c;
      $rootScope = $r;

describe("Unit: Testing Directives", function() {

  var $compile, $rootScope;

  beforeEach(module('App'));

  beforeEach(inject(
    ['$compile','$rootScope', function($c, $r) {
      $compile = $c;
      $rootScope = $r;
    }]
  ));

  it("should display the welcome text properly", function() {
    var element = $compile('<div data-app-welcome>User</div>')($rootScope);
    expect(element.html()).to.match(/Welcome/i);
  })

});
4

1 回答 1

1

尝试这样的事情,它对我有用:

 beforeEach(inject(function ($injector) {
        $rootScope = $injector.get('$rootScope');
        $scope = $rootScope.$new();
        $http = $injector.get('$http');
        $q = $injector.get('$q');
}));
于 2014-01-17T18:28:50.533 回答