Javascript Angularjs无法找到我的控制器
我使用angular mock注入控制器进行单元测试。我没有这样做,因为我一直得到以下错误Javascript Angularjs无法找到我的控制器,javascript,angularjs,unit-testing,Javascript,Angularjs,Unit Testing,我使用angular mock注入控制器进行单元测试。我没有这样做,因为我一直得到以下错误 [$injector:unpr] Unknown provider: PatientRecordsControllerProvider <- PatientRecordsController 还有我的测试用例 describe('PatientRecordsController:unit-testing', function () { beforeEach(module('pa
[$injector:unpr] Unknown provider: PatientRecordsControllerProvider <- PatientRecordsController
还有我的测试用例
describe('PatientRecordsController:unit-testing', function () {
beforeEach(module('patient_profile'));
it('timeline should be an array', inject(['PatientRecordsController',
function (controller) {
//Cant do stuff
}
]));
});
更新同样的程序在服务中也能正常工作。为什么?控制器必须使用
$Controller
服务进行实例化。下面的测试格式不是更干净吗
describe('PatientRecordsController:unit-testing', function () {
var controller;
beforeEach(function(){
module('patient_profile');
inject(function(_$controller_){
controller = _$controller_('PatientRecordsController', {});
});
});
it('timeline should be an array', function(){
//DO STUFF using controller
});
});
我听起来好像是提供商而不是控制器有问题。嗯,控制器是提供商,不是吗?那么,解决方案是什么?您是否忘记在测试中包含
患者档案
模块的脚本?@KhanhTO脚本加载良好。这可能是剧本的顺序吗?答案一点用都没有吗?哦,天哪!这很有魅力。但你的回答仍然不能确定我的方法有缺陷的原因。请您也解释一下好吗?正如我前面所说,您需要$controller
服务来实例化您在方法中没有使用的相关控制器。看看上面的答案中的inject
。但是如果它注册为服务,同样的事情也会发生。这是如何工作的?我可能听起来很傻:(你有一个有效的问题。是的,你不需要注入一个服务,它在控制器中使用,因为服务是单例的,一旦控制器在测试中实例化,就会自动提供/注入。
describe('PatientRecordsController:unit-testing', function () {
var controller;
beforeEach(function(){
module('patient_profile');
inject(function(_$controller_){
controller = _$controller_('PatientRecordsController', {});
});
});
it('timeline should be an array', function(){
//DO STUFF using controller
});
});