简体   繁体   English

如何在我的Karma / Mocha测试套件中测试Angular 1.5控制器?

[英]How can I test my Angular 1.5 controller in my Karma/Mocha test suite?

I'm trying to instantiate my controller in my Mocha test (using Karma as a runner) in order to test some of the controller methods. 我正在尝试在Mocha测试中实例化我的控制器(使用Karma作为运行程序),以便测试某些控制器方法。 I'm following something similar to this Angular ES6 Webpack setup . 我正在遵循与此Angular ES6 Webpack设置类似的内容

Here's what I'm trying: 这是我正在尝试的:

// controller.js

// also, the component that uses this controller
// declares some bindings, like "someData: '<'"

export default class SomeController {
  // notice the injection
  constructor($timeout) {

    // uses ng-annotate
    'ngInject';

  }
}

.

// test.spec.js (same dir as controller)

import SomeController from './controller.js'

describe('MyComponent', () => {
  let $rootScope;
  let $componentController;
  let $timeout;
  let makeController;

  beforeEach(window.module('app'));

  beforeEach(inject((_$componentController_, _$timeout_) => {
    $componentController = _$componentController_;
    $timeout = _$timeout_;
    makeController = () => {
      // I've tried the 2 options below
      // return new Controller($timeout)
      // return $componentController('myComponent', { $timeout }, { someData: [] });
    };
  }));

  describe('Controller', () => {
    it('does something', () => {

      // FAILS HERE

      $ctrl = makeController();

    });
  });
});

So, when I do new Controller() , it expects $timeout to be passed in as an argument to the constructor (it's supposed to be a dependency injection), and I can't figure out how to pass in someData binding. 因此,当我执行new Controller() ,它期望将$timeout作为构造函数的参数传递(应该是依赖注入),而我不知道如何传递someData绑定。

If I instead try the $componentController (as recommended by Angular 1.5 component docs ), I get this strange error: 如果我改为尝试$componentController (按照Angular 1.5 component docs的建议), 则会收到这个奇怪的错误:

compileProvider.preAssignBindingsEnabled is not a function

So, I looked up this method $compileProvider.preAssignBindingsEnabled and tried to set it to both true and false in my module config, but it still throws saying it's not a function. 因此,我查找了此方法$compileProvider.preAssignBindingsEnabled并尝试在我的模块配置中将其设置为truefalse ,但是仍然抛出该false ,它不是函数。 So I've hit a dead end. 所以我已经死胡同了。

Any ideas of how I can instantiate my controller to use it for testing? 关于如何实例化控制器以用于测试的任何想法?

Which version of angular are you using ? 您使用的是哪个版本的angular? Went into the same problem and it turned out that I had angular 1.5.7 and angular-mocks 1.5.10. 遇到了同样的问题,结果发现我有1.5.7的角度和1.5.10的角度模拟。 Put angular-mocks to 1.5.7 (same version of angular) fixed the problem. 将角球设置为1.5.7(角球的相同版本)可解决此问题。 preAssignBindingsEnabled has been added on 1.5.10 eg: https://github.com/angular/angular.js/blob/master/CHANGELOG.md preAssignBindingsEnabled已在1.5.10上添加,例如: https : //github.com/angular/angular.js/blob/master/CHANGELOG.md

You're going to have to downgrade to angular 1.5.5. 您将必须降级到1.5.5。 Anything higher than that will give you the "TypeError: $compileProvider.preAssignBindingsEnabled is not a function" error. 高于此值的任何错误都会给您带来“ TypeError:$ compileProvider.preAssignBindingsEnabled不是函数”错误。

Also make sure you that you're angular-mocks is the same version(1.5.5). 另外,请确保您使用的是相同的版本(1.5.5)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM