【问题标题】:Angular Controller Mock is not perisisting in my Mocha testAngular Controller Mock 在我的 Mocha 测试中没有持续存在
【发布时间】:2015-09-18 20:30:45
【问题描述】:

当前运行 Angular 1.2.x 与 karma-mocha 0.2.0 和 karma-chai 0.1.0。

以下是我尝试为基于Mentors 模块的MentorAvailabilityDashboardCtrl 编写一些简单的单元测试。单独运行时一切都通过了,但是当我按原样运行测试时,我收到一条错误消息,指出“错误:[ng:areq] Argument 'MentorAvailabilityDashboardCtrl' 不是函数,未定义”

这让我觉得 Mocha 或 AngularMock 在测试运行一次后被重置并且在之后未定义。

示例:

一次测试全部运行

  • 第一次测试 = 通过
  • 第二次测试 = 失败“MentorAvailabilityDashboardCtrl 未定义”
  • 第三次测试 = 失败“MentorAvailabilityDashboardCtrl 未定义”
  • 第 4 次测试 = 失败“MentorAvailabilityDashboardCtrl 未定义”

测试同时运行(注释掉其他测试)

  • 第一次测试 = 通过
  • 第二次测试 = 通过
  • 第三次测试 = 通过
  • 第四次测试 = 通过

问题:

为什么我的控制器在第一次测试运行后未定义,我能做些什么让它在每次测试运行时都保持不变?

我的代码:

'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('');
    });
  });

});

实际错误:

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)

【问题讨论】:

    标签: javascript angularjs unit-testing testing karma-mocha


    【解决方案1】:

    您似乎需要在 beforeEach 块中定义控制器,这可能与它有关。可能是范围界定问题。您可以尝试完全避免使用角度模拟,直接测试控制器功能。由于您使用的是 es6 模块,因此您可以将控制器函数直接导出为命名导出。

    export function MyController($scope, Flash) {
     //...
    }
    
    MyController.$inject = ['$scope', 'Flash'];
    
    angular.module('Mentors').controller('MyController')
    

    然后在测试中

    import sinon from 'sinon';
    import {MyController} from './my_controller'
    
    
    it('...', function() {
      let scope = {};
      let Flash = sinon.mock();
    
      MyController(scope, Flash);
    
      //... assertions
    })
    

    【讨论】:

    • 我收到以下错误错误:[$injector:nomod] 模块“导师”不可用!您要么拼错了模块名称,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。
    • 原来我只需要添加..., [])
    猜你喜欢
    • 1970-01-01
    • 2021-05-14
    • 2016-10-19
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    相关资源
    最近更新 更多