2015-05-29 19 views
6

Viene visualizzato questo errore comune Errore: [$injector:unpr] Unknown provider: $scopeProvider <- $scope con il mio caso di test. So che questo è comune e ci sono pochi altri thread che spiegano con le soluzioni. Ma davvero non sono riuscito a trovare una risposta al mio problema. Qualcuno può indicarmi la giusta direzione?

ViewMeetingCtrl,

(function() { 
    'use strict'; 
    angular.module('MyApp').controller('ViewMeetingCtrl', ViewMeetingCtrl); 

    ViewMeetingCtrl.$inject = ['$scope', '$state', '$http', '$translate', 'notificationService', 'meetingService', '$modal', 'meeting', 'attachmentService']; 

    function ViewMeetingCtrl($scope, $state, $http, $translate, notificationService, meetingService, $modal, meeting, attachmentService) { 
     $scope.meeting = meeting; 
     $scope.test = "testvalue"; 
     if (meeting.Status == 'Cancelled') 
     { 
      $scope.actionButtons = false; 
     } 
     else 
     { 
      $scope.actionButtons = true; 
     } 

     //more code 
    } 
})(); 

MeetingCtrlSpec.js

describe('ViewMeetingCtrl', function() { 
    var $rootScope, scope, $controller, meetingService; 

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

    beforeEach(inject(function ($rootScope, $controller, meetingService) { 
     scope = $rootScope.$new(); 
     $controller('ViewMeetingCtrl', { 
      meetingService: meetingService, 
      '$rootScope' : $rootScope, 
      scope: scope 
     });  

    })); 

    it('should change greeting value if name value is changed', function() { 
     //some assertion 
    }); 
}); 

traccia Errore:

Firefox 37.0.0 (Windows 8.1) ViewMeetingCtrl should change greeting value if name value is changed FAILED 
     Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- ViewMeetingCtrl 
     http://errors.angularjs.org/1.3.15/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20ViewMeetingCtrl 

     minErr/<@C:/Users/dell%20pc/Documents/Work/MyApp/WebApiRole/bower_components/angular/angular.js:63:12 
     createInjector/providerCache.$injector<@C:/Users/dell%20pc/Documents/Work/MyApp/WebApiRole/bower_components/ang 
ular/angular.js:4015:19 
     [email protected]:/Users/dell%20pc/Documents/Work/MyApp/WebApiRole/bower_components/angular/angular.js:4162:39 
     createInjector/instanceCache.$injector<@C:/Users/dell%20pc/Documents/Work/MyApp/WebApiRole/bower_components/ang 
ular/angular.js:4020:28 
     [email protected]:/Users/dell%20pc/Documents/Work/MyApp/WebApiRole/bower_components/angular/angular.js:4162:39 
     [email protected]:/Users/dell%20pc/Documents/Work/MyApp/WebApiRole/bower_components/angular/angular.js:4194:1 
     [email protected]:/Users/dell%20pc/Documents/Work/MyApp/WebApiRole/bower_components/angular/angular.js:4211:27 
     $ControllerProvider/this.$get</<@C:/Users/dell%20pc/Documents/Work/MyApp/WebApiRole/bower_components/angular/an 
gular.js:8501:18 
     angular.mock.$ControllerDecorator</<@C:/Users/dell%20pc/Documents/Work/MyApp/WebApiRole/node_modules/angular-mo 
cks/angular-mocks.js:1878:12 
     @C:/Users/dell pc/Documents/Work/MyApp/FLIS.Client.Tests/test/company/MeetingCtrlSpec.js:8:1 
     [email protected]:/Users/dell%20pc/Documents/Work/MyApp/WebApiRole/bower_components/angular/angular.js:4203:14 
     [email protected]:/Users/dell%20pc/Documents/Work/MyApp/WebApiRole/node_modules/angular-mocks/angular-mocks.js:2436:11 
     [email protected]:/Users/dell%20pc/Documents/Work/MyApp/WebApiRole/node_modules/angular-mocks/angular-mocks 
.js:2407:25 
     @C:/Users/dell pc/Documents/Work/MyApp/Client.Tests/test/company/MeetingCtrlSpec.js:6:16 
     @C:/Users/dell pc/Documents/Work/MyApp/Client.Tests/test/company/MeetingCtrlSpec.js:1:1 
Firefox 37.0.0 (Windows 8.1): Executed 3 of 3 (1 FAILED) (0.094 secs/0.091 secs) 
+1

Typo! Dovrebbe essere '$ scope: scope' si è perso un dollaro e non è necessario iniettare' $ rootScope' dato che non si è dipendenti, anche se questo non è il problema. – PSL

+0

@PSL, molto obbligato per la tua risposta. questo ha risolto il mio problema, ma alla fine ne ho portato un altro. Dice 'Errore: [$ injector: unpr] Provider sconosciuto: meetingProvider <- meeting <- ViewMeetingCtrl'. puoi dare qualcosa per questo errore? Perché non ho nulla chiamato meetingProvider da nessuna parte nella mia soluzione. – Malik

risposta

6

Sostituire

$controller('ViewMeetingCtrl', { 
     meetingService: meetingService, 
     '$rootScope' : $rootScope, 
     scope: scope 
    }); 

da

$controller('ViewMeetingCtrl', { 
     meetingService: meetingService, 
     $scope: scope 
    }); 

Il regolatore deve essere iniettato con un argomento di nome $ portata, non campo di applicazione. E $ rootScope non fa parte dei collaboratori iniettati del controller.

+0

grazie per la risposta. tuttavia, il test case modificato mi dà un altro errore dicendo: Errore: [$ injector: unpr] Provider sconosciuto: meetingProvider <- meeting <- ViewMeetingCtrl'. Ma non ho niente chiamato meetingProvider. ? – Malik

+1

Significa solo che non è stato caricato alcun modulo contenente un servizio denominato 'riunione'. Ogni servizio ha un provider associato (anche se non definito in modo esplicito) –

+0

'ViewMeetingCtrl. $ Inject = ['$ scope', '$ stato', '$ http', '$ translate', 'notificationService', 'meetingService', ' $ modal ',' meeting ',' attachmentService ']; ' quelle sono le dipendenze inietti nel ctrl. vuol dire che devo iniettarli anche nel test case? – Malik

-1

stesso errore ho dovuto dimenticare di mettere '$' prima scope in

app.controller("loginCtrl", function(**$**scope){ 

}) 
+2

Ciao, benvenuto in SO. Per favore non aggiungere una risposta che ripete solo una già esistente. Invece, upvote quello che è già lì. So che per il momento non hai abbastanza rep per questo (15), ma in questo caso l'approccio migliore è quello di aggiungere questa pagina ai segnalibri e, quando hai un rappresentante sufficiente, torna qui e upvote. Altrimenti, per quanto frustrante possa essere (siamo stati tutti lì!), Lasciamolo andare. Ma per favore non pubblicare una risposta che non aggiunge nulla a ciò che è già lì. Grazie! –

Problemi correlati