为控制器创建测试文件 - karma + jasmine

时间:2017-04-26 12:11:29

标签: angularjs karma-jasmine

我正在学习在我的角度应用程序中为控制器编写测试。

我有一个类似这样的测试文件

describe('Controller', function() {
beforeEach(module('AngularApp'));

var scope, controller;

    beforeEach(inject(function($rootScope, $controller) {
        scope = $rootScope.$new();
        controller = $controller('someController', {$scope: scope});
    }));

    describe('someController',function(){

      it('Testing Scope.show is defined',function() {
          expect(scope.show).toBeDefined();
  });
});

});

这里我测试范围show是否被声明。

现在我想测试范围是否等于某个值。

如果我在现有it下方写下新的it,我会收到此错误

A unique name is required for a Tool Definition thrown

describe('someController',function(){

  it('Testing Scope.show is defined',function() {
      expect(scope.show).toBeDefined();
  });
  it('Testing Scope.show is true',function() {
      expect(scope.show).toEqual("xyz");
  });

});

如果我想测试下一个范围值,请说例如' list'。

如何在同一个文件中为其编写测试。

当我添加另一个描述和写测试时,我得到的错误与上面提到的相同。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。使测试通过。 我重写抛出错误的常量函数。我希望这会有所帮助。

beforeEach(module('app', function($provide) {
    $provide.constant("taRegisterTool", function () { });
}));