angularjs - कोणीय नकली मेरे मॉड्यूल निर्भरता को इंजेक्षन करने में विफल रहता है




jasmine karma-runner (2)

आप परीक्षण में fooApp मॉड्यूल को पुन: परिभाषित कर रहे हैं।

आपको इस तरह परीक्षण कोड में कोणीय से लोड करना होगा:

angular.mock.module('ngRoute', 'ngAnimate', 'hmTouchEvents', 'fooApp');

मैं अपने एप्लिकेशन fooApp लिए एक कोणीय नियंत्रक का परीक्षण करना चाहता fooApp , जिसे निम्न के रूप में परिभाषित किया गया है:

var fooApp = angular.module('fooApp', [ 'ngRoute', 'ngAnimate', 'hmTouchEvents' ]);
...

नियंत्रक, MainCtrl परिभाषित किया गया है:

"use strict";

fooApp.controller('MainCtrl', function ($scope, $rootScope, fooService) {
    ...
}

इसलिए मैंने एक परीक्षण बनाने के कई तरीकों का परीक्षण किया है, जैसे यह एक:

'use strict';

describe('MainController test', function () {

    var scope;
    var controller;

    beforeEach(function () {
            angular.mock.module('ngRoute', []);
            angular.mock.module('ngAnimate', []);
            angular.mock.module('hmTouchEvents', []);
            angular.module('cwfApp', [ 'ngRoute', 'ngAnimate', 'hmTouchEvents' ]);

            angular.mock.inject(function ($rootScope, $controller) {
                scope = $rootScope.$new();
                controller = $controller('MainCtrl', {
                    $scope: scope
                });
            });
        });

        it('should display a list', function () {
            console.log('-------------- Run Test 1 | ' + scope);
            expect(scope.currentStep).toBe(1);
        });

});

और परिणाम:

        Error: [$injector:modulerr] http://errors.angularjs.org/1.2.16-build.64+sha.245de33/$injector/modulerr?p0=undefined&p1=Error%3A%20%5Bng%3Aareq%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.2.16-build.64%2Bsha.245de33%2Fng%2Fareq%3Fp0%3Dfn%26p1%3Dnot%2520a%2520function%252C%2520got%2520undefined%0A%20%20%20%20at%20Error%20(%3Canonymous%3E)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A6%3A471%0A%20%20%20%20at%20wb%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A18%3A360)%0A%20%20%20%20at%20Qa%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A18%3A447)%0A%20%20%20%20at%20nc%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A31%3A191)%0A%20%20%20%20at%20Object.d%20%5Bas%20invoke%5D%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A33%3A176)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A32%3A254%0A%20%20%20%20at%20Array.forEach%20(native)%0A%20%20%20%20at%20r%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A7%3A298)%0A%20%20%20%20at%20e%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A32%3A9)
        at Error (<anonymous>)
        at d:/dev/foo/app/bower_components/angular/angular.min.js:6:471
        at d:/dev/foo/app/bower_components/angular/angular.min.js:32:400
        at Array.forEach (native)
        at r (d:/dev/foo/app/bower_components/angular/angular.min.js:7:298)
        at e (d:/dev/foo/app/bower_components/angular/angular.min.js:32:9)
        at Object.$b [as injector] (d:/dev/foo/app/bower_components/angular/angular.min.js:35:98)
        at workFn (d:/dev/foo/app/bower_components/angular-mocks/angular-mocks.js:2142:52)
        at Object.window.inject.angular.mock.inject [as inject] (d:/dev/foo/app/bower_components/angular-mocks/angular-mocks.js:2133:37)
        at null.<anonymous> (d:/dev/foo/test/jasmine/todo.test.js:15:22)
    TypeError: Cannot read property 'currentStep' of undefined
        at null.<anonymous> (d:/dev/foo/test/jasmine/todo.test.js:25:21)
Chrome 31.0.1650 (Windows 7): Executed 1 of 1 (1 FAILED) ERROR (0.023 secs / 0.015 secs)        

मैंने पहले भी प्रत्येक के साथ परीक्षण किया है beforeEach(angular.mock.module('cwfApp')); (पिछला कोड में पहले beforeEach के पहले), लेकिन त्रुटि लगभग एक ही है।

मेरी karma.conf.js फ़ाइल के बारे में, मैं फाइलों की इस सूची को सेट करता हूं:

    files: [
        'app/bower_components/angular/angular.min.js',
        'app/bower_components/angular-route/angular-route.min.js',
        'app/bower_components/hammerjs/hammer.min.js',
        'app/bower_components/angular-hammer/angular-hammer.js',
        'app/bower_components/angular-mocks/angular-mocks.js',

        'app/js/foo-application.js',
        'app/js/foo-controllers.js',
        'app/js/foo-services.js',
        'app/js/foo-router.js',

        'test/jasmine/*.js'
    ],

इंजेक्शन विफल हो रहा है, लेकिन मुझे वास्तव में यह समझ नहीं आ रहा है कि मेरे कॉन्फ़िगरेशन में क्या गुम या गलत है। उपरोक्त स्टैकट्र्रेस बहुत अधिक स्पष्टीकरण नहीं देता ...

कोई उपाय?

मैं कोणीय का उपयोग कर रहा हूँ 1.2.8

सादर

एंजिनियर द्वारा दिए गए कोड के साथ संपादित करें:

beforeEach(angular.mock.module('fooApp'));

beforeEach(angular.mock.inject(function($rootScope, $controller) {
    scope = $rootScope.$new();
    controller = $controller('MainCtrl', {
        $scope: scope
    });
}));

it('should display a list', function () {
    console.log('-------------- Run Test 1 | ' + scope);
    expect(scope.currentStep).toBe(1);
});

त्रुटि लगभग समान है:

    Error: [$injector:modulerr] http://errors.angularjs.org/1.2.16-build.64+sha.245de33/$injector/modulerr?p0=cwfApp&p1=Error%3A%20%5B%24injector%3Amodulerr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.2.16-build.64%2Bsha.245de33%2F%24injector%2Fmodulerr%3Fp0%3DngAnimate%26p1%3DError%253A%2520%255B%2524injector%253Anomod%255D%2520http%253A%252F%252Ferrors.angularjs.org%252F1.2.16-build.64%252Bsha.245de33%252F%2524injector%252Fnomod%253Fp0%253DngAnimate%250A%2520%2520%2520%2520at%2520Error%2520(%253Canonymous%253E)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F5961971009303638e9ad386869316e8c83f67e56%253A6%253A471%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F5961971009303638e9ad386869316e8c83f67e56%253A20%253A260%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F5961971009303638e9ad386869316e8c83f67e56%253A21%253A262%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F5961971009303638e9ad386869316e8c83f67e56%253A32%253A69%250A%2520%2520%2520%2520at%2520Array.forEach%2520(native)%250A%2520%2520%2520%2520at%2520r%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F5961971009303638e9ad386869316e8c83f67e56%253A7%253A298)%250A%2520%2520%2520%2520at%2520e%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F5961971009303638e9ad386869316e8c83f67e56%253A32%253A9)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F5961971009303638e9ad386869316e8c83f67e56%253A32%253A86%250A%2520%2520%2520%2520at%2520Array.forEach%2520(native)%0A%20%20%20%20at%20Error%20(%3Canonymous%3E)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A6%3A471%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A32%3A400%0A%20%20%20%20at%20Array.forEach%20(native)%0A%20%20%20%20at%20r%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A7%3A298)%0A%20%20%20%20at%20e%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A32%3A9)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A32%3A86%0A%20%20%20%20at%20Array.forEach%20(native)%0A%20%20%20%20at%20r%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A7%3A298)%0A%20%20%20%20at%20e%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A32%3A9)
        at Error (<anonymous>)
        at d:/dev/foo/app/bower_components/angular/angular.min.js:6:471
        at d:/dev/foo/app/bower_components/angular/angular.min.js:32:400
        at Array.forEach (native)
        at r (d:/dev/foo/app/bower_components/angular/angular.min.js:7:298)
        at e (d:/dev/foo/app/bower_components/angular/angular.min.js:32:9)
        at Object.$b [as injector] (d:/dev/foo/app/bower_components/angular/angular.min.js:35:98)
        at workFn (d:/dev/foo/app/bower_components/angular-mocks/angular-mocks.js:2142:52)
    TypeError: Cannot read property 'currentStep' of undefined
        at null.<anonymous> (d:/dev/foo/test/jasmine/todo.test.js:20:21)

मैं अपनी समस्या को पुन: उत्पन्न करने के लिए एक बेला बनाने की कोशिश करूंगा ...


इस तरह से प्रयास करें:

describe('MainController test', function () {
    var scope;
    var controller;

    beforeEach(angular.mock.module('fooApp'));
    beforeEach(angular.mock.inject(function($rootScope, $controller) {
        scope = $rootScope.$new();
        controller = $controller('MainCtrl', {
            $scope: scope
        });
    });
    it('should display a list', function () {
        console.log('-------------- Run Test 1 | ' + scope);
        expect(scope.currentStep).toBe(1);
    });
});

यह बात समझने की जरूरत है, यदि आप अपनी निर्भरता (जैसे angular.module('fooApp', [ 'ngRoute', 'ngAnimate', 'hmTouchEvents' ]) मॉड्यूल angular.module('fooApp', [ 'ngRoute', 'ngAnimate', 'hmTouchEvents' ]) साथ कुछ मॉड्यूल घोषित करते हैं, तो जब आप मॉड्यूल इंजेक्ट करते हैं ( fooApp ) अपने परीक्षणों पर, आपको निर्भरता मॉड्यूल को इंजेक्षन करने की आवश्यकता नहीं है।