اختبار jasmine angularjs - الوسيطة "PhoneListCtrl" ليست وظيفة، ولم يتم تعريفها
-
21-12-2019 - |
سؤال
عند إجراء اختبار angularjs + Jasmine + Karma، حصلت على الخطأ التالي:
نص الاختبار الخاص بي هو:
describe('PhoneCat controllers', function() {
describe('PhoneListCtrl', function(){
it('should create "phones" model with 3 phones', inject(function($controller) {
var scope = {},
ctrl = $controller('PhoneListCtrl', { $scope: scope });
expect(scope.phones.length).toBe(3);
}));
});
});
هذا الرمز هو مجرد نسخة من البرنامج التعليمي الرسمي لـ AngularJS هنا:http://code.angularjs.org/1.2.0-rc.3/docs/tutorial/step_02
إليك جزء من ملف karma.conf.js الخاص بي:
// list of files / patterns to load in the browser
files: [
'js/bower_components/angular/angular.js',
'js/bower_components/angular/ngular-mocks.js',
'js/app/controllers.js',
'test/unit/*.js'
],
الخطأ هو قائمة الهاتفCtrl لم يتم تعريفه، ولكن أعتقد أنه تم تعريفه وتحميله في الكود أعلاه.ماذا تتوقع المشكلة؟شكرًا!
المحلول
جزء تهيئة الوحدة مفقود في اختبار الوحدة الخاصة بك.يجب عليك الاتصال module('phonecatApp')
قبل الاتصال لأول مرة inject()
.يجب أن يبدو رمز اختبار الوحدة الخاص بك في هذه الحالة كما يلي:
describe('PhoneCat controllers', function() {
describe('PhoneListCtrl', function(){
beforeEach(function() {
module('phonecatApp'); // <= initialize module that should be tested
});
it('should create "phones" model with 3 phones', inject(function($controller) {
var scope = {},
ctrl = $controller('PhoneListCtrl', { $scope: scope });
expect(scope.phones.length).toBe(3);
}));
});
});
أين phonecatApp
هو اسم الوحدة حيث قمت بتعريف الخاص بك PhoneListCtrl
مراقب.
كما أن البرنامج التعليمي الذي تستخدمه قديم، فهو مخصص لإصدار غير مستقر من Angular (1.2.0-rc.3).إليك نسخة محدثة من نفس البرنامج التعليمي لأحدث إصدار من Angular: http://docs.angularjs.org/tutorial/step_02
نصائح أخرى
هذا يعمل بالنسبة لي
giveacodicetagpre.