angularjs - 类型错误 : 'undefined' is not a function when using spyOn with Karma

标签 angularjs jasmine karma-runner

我一直在使用 Yeoman 设置项目,并尝试构建一个应用功能切换的 Angular 模块项目。我对单元级别的 Jasmine 测试很着迷,这是我的测试之一:

var scope, Rules;
var elm;
// load the controller's module
beforeEach(function() {
  module('featureToggle.directives', function($provide) {
    $provide.decorator('Rules', function($delegate) {
      $delegate.resolveByName = jasmine.createSpy();

      return $delegate;
    });
  });

  inject(function(_Rules_) {
    Rules = _Rules_;
  });
});

beforeEach(inject(function($rootScope, $compile) {
  scope = $rootScope.$new();
  elm = angular.element(
    '<h1 feature-toggle feature-name="hello">' +
    'Hello World' +
    '</h1>');

  scope = $rootScope;
  $compile(elm)(scope);
  scope.$digest();
}));

it('should render the content when the feature is enabled', function() {
  jasmine.spyOn(Rules, 'resolveByName').andReturn(false);

  var contents = elm.find('h1');
  expect(contents.eq(0)).toHaveClass('hidden');
});

我遇到了一个奇怪的失败。

TypeError: 'undefined' is not a function (evaluating 'jasmine.spyOn(Rules, 'resolveByName')')

karma.conf 非常普通。

frameworks = ['jasmine'];

// list of files / patterns to load in the browser
files = [
   JASMINE,
   JASMINE_ADAPTER,
   'app/components/angular/angular.js',
   'app/components/angular-mocks/angular-mocks.js',
   'src/*.js',
   'src/**/*.js',
   'test/mock/**/*.js',
   'test/spec/**/*.js'
];

我大体上使用 PhantomJS,但在 Chrome 中存在相同的问题(相同错误的不同方言)。

使用 $log,实际上没有什么是未定义的。所以我很迷茫。

最佳答案

jasmine.spyOn() 更改为 spyOn()

顺便说一句,不确定为什么要两次监视相同的方法。

关于angularjs - 类型错误 : 'undefined' is not a function when using spyOn with Karma,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18257092/

相关文章:

javascript - 无法访问父范围的属性,但是我可以通过该属性链看到

javascript - 如何从 Angularjs 中的选择选项生成的动态 url 中删除双引号?

testing - 如何使用 Jasmine 和 Protractor 在通过的测试中编写自定义消息?

javascript - jasmine describe函数中的beforeEach和afterEach自动添加代码

javascript - 如何编写 Jasmine 自定义匹配器

javascript - 如何运行单个 Testacular 单元测试?

javascript - 在输入更改时发送多个 http 请求并应用最后的更改

javascript - 如何以 Angular 形式将地址或任何字段复制到另一个字段?

angularjs - karma-browserify 在单元测试中找不到模块

AngularJS 和 Karma,无法调用未定义的方法 'module'