在我的Mocha测试中,Angular Controller Mock没有持续存在
Angular Controller Mock is not perisisting in my Mocha test
当前运行Angular 1.2。X与业力摩卡0.2.0和业力chai 0.1.0。
下面是我试图为我的MentorAvailabilityDashboardCtrl
写一些简单的单元测试,这是基于Mentors
模块。当单独运行时,一切都通过,但当我运行测试时,我得到一个错误,说明"错误:[ng:areq]参数'MentorAvailabilityDashboardCtrl'不是一个函数,得到未定义"
这让我认为Mocha或AngularMock在测试运行一次后被重置,并且之后未定义。
例子:
测试一次全部运行
- 第一次测试=通过
- 第二次测试=失败"MentorAvailabilityDashboardCtrl got undefined"
- 第三次测试=失败"MentorAvailabilityDashboardCtrl got undefined"
- 第4次测试=失败"MentorAvailabilityDashboardCtrl got undefined"
测试一次运行所有测试(注释掉其他测试)
- 第一次测试=通过
- 第二次测试=通过
- 第三次测试=通过
- 第4次测试=通过
问题:
为什么我的控制器在第一次测试运行后未定义,我能做些什么让它在每次测试运行中持续存在?
我代码:'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)
看起来您需要在beforeEach块中的控制器定义,这可能与它有关。可能是范围问题。你可以尝试避免所有的角模拟,直接测试控制器功能。因为你使用的是es6模块,所以你可以直接将控制器函数导出为命名的export。
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
})
相关文章:
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- 如何使用Spring MVC将Facebook返回的响应数据保存在Java类中
- 用于检查数组中是否存在元素的javascript自定义方法
- 未捕获的类型错误:无法读取属性'name'即使它存在,也无法定义
- 正在将事件处理程序添加到不存在的类
- 是否存在React Native“;WEB代码安全防护”;
- 一个密码测试程序,如果存在空格,它会提醒用户
- 验证会话中是否存在对象's数组
- javascript如果图像不存在don't加载它
- 如何查找值是否存在于二叉树中
- IE9的HTML5 Canvas getImageData()函数存在问题
- 由于响应中不存在“Access Control Allow Origin”标头,跨域请求停止工作
- CORS-服务器端cookie没有保存在chrome浏览器上
- 检查是否存在使用chrome扩展的javascript库
- 是否存在Javascript Liferay Service库的文档?如何处理错误情况
- 测试mongo脚本中是否存在参数
- 存在每个时间元素的javascript事件
- 在我的Mocha测试中,Angular Controller Mock没有持续存在