茉莉花单元测试使用注射

时间:2014-01-06 17:26:03

标签: karma-runner karma-jasmine

在这个测试中,我不确定为什么你需要将角度变量设置为这两行中的注入参数。是因为注入不会自动分配$ 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);
  })

});

1 个答案:

答案 0 :(得分:1)

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

 beforeEach(inject(function ($injector) {
        $rootScope = $injector.get('$rootScope');
        $scope = $rootScope.$new();
        $http = $injector.get('$http');
        $q = $injector.get('$q');
}));