问题描述
我有以下code茉莉测试。 ProjectConfigurationCtrl是控制器我试图测试的名称。
I have a following code for Jasmine testing. ProjectConfigurationCtrl is the name of controller I am trying to test.
describe('Unit test: ProjectConfiguration controller', function() {
var scope, routeParams, infraService, controllerToTest;
// some stuff declaration skipped...
beforeEach(inject(function($injector) { // get all dependences
routeParams = $injector.get('$routeParams');
infraService = $injector.get('InfraService');
$rootScope = $injector.get('$rootScope');
scope = $rootScope.$new();
scope.projectData = fakedDto;
var $controller = $injector.get('$controller');
controllerToTest = function() {
return $controller('ProjectConfigurationCtrl', { //
'$scope': scope
});
};
}));
// ...
it('saves new project successfully', function() {
var controller = controllerToTest();
// here, I try to call test function in and check results...
scope.clickUpdate(fakedDto); // <-- controller defines this function in given scope, so I hope it runs like this in test.
});
}); // describe block ends
这code错误结束(因果报应/茉莉输出):
this code ends up in error (karma/jasmine output):
minErr/<@C:/src/ClientApp/client/bower_components/angular/angular.js:78
loadModules/<@C:/src/ClientApp/client/bower_components/angular/angular.js:3703
forEach@C:/src/ClientApp/client/bower_components/angular/angular.js:322
loadModules@C:/src/ClientApp/client/bower_components/angular/angular.js:3668
createInjector@C:/src/ClientApp/client/bower_components/angular/angular.js:3608
workFn@C:/src/ClientApp/client/bower_components/angular-mocks/angular-mocks.js:2144
TypeError: controllerToTest is not a function in C:/src/ClientApp/tests/unit/controllers/projectconfigcontroller.test.js (line 85)
@C:/src/ClientApp/tests/unit/controllers/projectconfigcontroller.test.js:85
可能是什么原因?
what could be the reason?
推荐答案
c0bra,叶廖氏在这里都有很好的点。
c0bra and Ye Lio both have good points here.
c0bra是正确的,你有没有所谓的业力/茉莉辅助方法,模块,包括含有ProjectConfigurationCtrl模块。
c0bra is right in that you have not called the karma/jasmine helper method 'module' to include the module that contains 'ProjectConfigurationCtrl'.
您需要添加类似以下内容:
You need to add something like the following:
beforeEach( module( 'module.containing.ProjectConfigurationCtrl' ) );
如果你不这样做运行上面的脚本时,你会得到像下面这样的错误:
If you don't do this you will get an error like the following when running the script above:
错误:[NG:AREQ]参数'ProjectConfigurationCtrl不是一个函数,得到了不确定
Error: [ng:areq] Argument 'ProjectConfigurationCtrl' is not a function, got undefined
不过,您所看到的错误类型错误:controllerToTest不是一个函数表示,不知怎的,controllerToTest是越来越设置其他地方的东西,不是一个函数
However, the error you are seeing "TypeError: controllerToTest is not a function" indicated that somehow controllerToTest is getting set somewhere else to something that is not a function.
如果没有这解决您的问题,请发表您的测试一个新的,完整版瓦特/替代上述建议。
If none of this fixes your problem please post a new, complete version of your test w/ the above suggestions in place.
这篇关于控制器在未声明茉莉花测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!