简体   繁体   English

Angular Controller Mock 在我的 Mocha 测试中不存在

[英]Angular Controller Mock is not perisisting in my Mocha test

Currently running Angular 1.2.x with karma-mocha 0.2.0 and karma-chai 0.1.0.目前使用 karma-mocha 0.2.0 和 karma-chai 0.1.0 运行 Angular 1.2.x。

The following is my attempt to write some simple Unit Tests for my MentorAvailabilityDashboardCtrl which is based off the Mentors module.以下是我尝试为基于Mentors模块的MentorAvailabilityDashboardCtrl编写一些简单的单元测试。 Everything passes when individually run, but when I run the test as is I get an error stating "Error: [ng:areq] Argument 'MentorAvailabilityDashboardCtrl' is not a function, got undefined"单独运行时一切都会通过,但是当我按原样运行测试时,我收到一条错误消息,指出“错误:[ng:areq] 参数‘MentorAvailabilityDashboardCtrl’不是函数,未定义”

It makes me think that Mocha or the AngularMock gets reset after the test get run once and is undefined after.这让我认为 Mocha 或 AngularMock 在测试运行一次后被重置,之后未定义。

Example:例子:

Test running all at once一次运行全部测试

  • 1st test = Pass第一次测试 = 通过
  • 2nd test = Fail "MentorAvailabilityDashboardCtrl got undefined"第二个测试 = 失败“MentorAvailabilityDashboardCtrl 未定义”
  • 3rd test = Fail "MentorAvailabilityDashboardCtrl got undefined"第三次测试 = 失败“MentorAvailabilityDashboardCtrl 未定义”
  • 4th test = Fail "MentorAvailabilityDashboardCtrl got undefined"第 4 次测试 = 失败“MentorAvailabilityDashboardCtrl 未定义”

Testing running all at once (commenting other test out)测试同时运行(注释掉其他测试)

  • 1st test = Pass第一次测试 = 通过
  • 2nd test = Pass第二次测试 = 通过
  • 3rd test = Pass第三次测试 = 通过
  • 4th test = Pass第 4 次测试 = 通过

Question:题:

Why is my controller going undefined after the first test run and Is there anything I can do have it persist for each test run?为什么我的控制器在第一次测试运行后未定义,我能做些什么让它在每次测试运行时都保持不变?

My Code:我的代码:

'use strict';
/* globals gon: false */

import angular from 'angular';

describe('MentorAvailabilityDashboardCtrl', function() {
  let createController, $scope;

  beforeEach(function() {
    angular.module('Mentors', []);
    require('./mentor_availability_dashboard')
    angular.mock.module('Mentors');
  });

  beforeEach(angular.mock.inject(function ($rootScope, $controller) {
      function MockMentorProfile() {}
      function MockFlash() {}

      $scope = $rootScope.$new()
      createController = $controller('MentorAvailabilityDashboardCtrl', {
        $scope: $scope,
        MentorProfile: MockMentorProfile,
        Flash: MockFlash
      });
  }));

  describe('validation checks', function() {
    it('should invalidate form fields on initialization', function() {
      expect($scope.validatedFields()).to.eq(false);
    });

    it('should validate specifc field on initialization', function() {
      $scope.courseFilter = 'Rails';

      expect($scope.fieldValidated($scope.courseFilter)).to.eq(true);
    });

    it('should validate form fields on completion', function() {
      $scope.courseFilter = 'Rails';
      $scope.osFilter = 'Windows';
      $scope.studentFilter = { student: 'Billy' };
      $scope.paceFilter = '12 weeks';
      $scope.startFilter = { monday: 'Monday' };

      expect($scope.validatedFields()).to.eq(true);
    });

    it('should be able to click form with clearForm()', function() {
      $scope.courseFilter = 'Rails';
      $scope.clearForm()

      expect($scope.courseFilter).to.eq('');
    });
  });

});

Actual Error:实际错误:

PhantomJS 1.9.8 (Mac OS X 0.0.0) MentorAvailabilityDashboardCtrl "before each" hook: workFn for "should validate specifc field on initialization" FAILED
    Error: [ng:areq] Argument 'MentorAvailabilityDashboardCtrl' is not a function, got undefined
    http://errors.angularjs.org/1.2.26/ng/areq?p0=MentorAvailabilityDashboardCtrl&p1=not%20a%20function%2C%20got%20undefined
        at assertArg (/Users/bdoug/Bloc/vendor/assets/bower_components/angular/angular.js:1509)
        at assertArgFn (/Users/bdoug/Bloc/vendor/assets/bower_components/angular/angular.js:1520)
        at /Users/bdoug/Bloc/vendor/assets/bower_components/angular/angular.js:7278
        at /Users/bdoug/Bloc/frontend/test/tests_index.js:15072 <- webpack:///frontend/legacy_org/mentors/mentor_availability_dashboard.test.js:23:8
        at invoke (/Users/bdoug/Bloc/vendor/assets/bower_components/angular/angular.js:3966)
        at workFn (/Users/bdoug/Bloc/vendor/assets/bower_components/angular-mocks/angular-mocks.js:2161)

It seems like you are requiring the controller definition in a beforeEach block and that might have something to do with it.似乎您需要在 beforeEach 块中定义控制器,这可能与它有关。 Probably a scoping issue.可能是范围问题。 You might try avoiding angular mocks alltogether and just test the controller function directly.您可以尝试完全避免角度模拟,而直接测试控制器功能。 Since you are using es6 modules you can export the controller function directly as a named export.由于您使用的是 es6 模块,因此您可以将控制器功能直接导出为命名导出。

export function MyController($scope, Flash) {
 //...
}

MyController.$inject = ['$scope', 'Flash'];

angular.module('Mentors').controller('MyController')

Then in the test然后在测试中

import sinon from 'sinon';
import {MyController} from './my_controller'


it('...', function() {
  let scope = {};
  let Flash = sinon.mock();

  MyController(scope, Flash);

  //... assertions
})

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

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