Karma-Jasmine测试中未定义控制器

时间:2018-05-01 14:02:07

标签: angularjs karma-jasmine

所以,到目前为止,我已经在这个奇怪的错误中磕磕绊绊了几天。希望你们能帮助在这条隧道中散发一些光芒。我真的很抱歉打扰你,但错误日志没有帮助。此外,类似的文件(如果需要我可以提供)具有相同的测试,以相同的方式编写,并且它们可以完美地工作。

我在karma-jasmine我的项目中进行过单元测试。除了这个文件外,一切顺利:

'use strict';

angular.module('myApp.parkinput', ['ngRoute', 'Parklot', 'City','ngFileUpload'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/parkinput', {
    templateUrl: 'parkinput/parkinput.html',
    controller: 'parkinputCtrl'
  });
}])

.controller('parkinputCtrl', ['$scope', 'NgMap', 'Parklot', 'City', '$location','Upload','$window', function($scope, NgMap, Parklot, City, $location,Upload,$window) {

  $scope.googleMapsUrl="https://maps.googleapis.com/maps/api/js?key=MYKEY";

  var map;

  $scope.newParkZone = {};
  $scope.newPicture = {};
  $scope.marker = {id:'newParkZone', name: 'newParkZone', position:null};

  $scope.cities = City.getCities().then(
    function(response){
       $scope.cities = response.data;
    },
    function(error){
       console.log(error);
    });

  NgMap.getMap().then(
    function(evtMap) {
       map = evtMap;
    },
    function(error){
      //console.log(error);
    });

  $scope.setCenter = function(event, city) {
    if(city)
    {
       //preparing the latlen obj to change the map center
       var coords = $scope.newParkZone.City.GPS_Coordinates_Center.split(",");
      map.setCenter({
         lat : coords[0]*1,
         lng : coords[1]*1
      });
      return;
     }
     map.setCenter(event.latLng);
     $scope.marker.position = event.latLng;
     $scope.newParkZone.coords = event.latLng.lat() + "," + event.latLng.lng();
  };

  $scope.submit = function () {
    $scope.newParkZone.picture = "";
    var newParklot = Parklot.create($scope.newParkZone);
    if ($scope.upload_form.file.$valid && $scope.file) { 
      $scope.upload($scope.file, newParklot); //call upload function
    }
  }

  $scope.upload = function (file, parklot) {
    Upload.upload({
      url: 'http://localhost:3000/image', 
      data:{file:file, parklot:parklot} 
    }).then(
      function (resp) {
        if(resp.data.error_code === 0){ 
          $window.alert('Success ' + resp.config.data.file.name + 'uploaded. Response: ');
        } else {
          $window.alert('an error occured');
        }
      },
      function (resp) { 
        $window.alert('Error status: ' + resp.status);
      });
  };

}]);

应按如下方式进行测试:

describe('Park Input Module', function() {

    beforeEach(module('myApp.parkinput'));

    describe('Route Configuration', function() {
        var $scope = null;
        var ctrl = null;

        it('should be correct', function() {

            inject(function($route, $location, $rootScope, $httpBackend) {

                expect($route.current).toBeUndefined();

                $httpBackend.expectGET('parkinput/parkinput.html').respond(200);
                $location.path('/parkinput');
                $rootScope.$digest();

                expect($route.current.templateUrl).toBe('parkinput/parkinput.html');
                expect($route.current.controller).toBe('parkinputCtrl');

            });
        }); 
    });

    describe('controller', function(){
        var $controller;

        beforeEach(inject(function(_$controller_, $rootScope){
            $controller = _$controller_;
        }));

        it('should be defined', function(){
            var $scope = {};
            var controller = $controller('parkinputCtrl', { $scope: $scope});
            expect(controller).toBeDefined()
        });
    })

});

以下是错误:

`

Firefox 59.0.0 (Windows 10 0.0.0) Park Input Module Route Configuration should be correct FAILED
        minErr/<@node_modules/angular/angular.js:116:12
        loadModules/<@node_modules/angular/angular.js:5023:15
        forEach@node_modules/angular/angular.js:408:11
        loadModules@node_modules/angular/angular.js:4983:5
        createInjector@node_modules/angular/angular.js:4900:19
        WorkFn@node_modules/angular-mocks/angular-mocks.js:3173:44
        angular.mock.inject@node_modules/angular-mocks/angular-mocks.js:3153:35
        @app/parkinput/parkinput.spec.js:11:13
        <Jasmine>
Firefox 59.0.0 (Windows 10 0.0.0) Park Input Module controller should be defined FAILED
        minErr/<@node_modules/angular/angular.js:116:12
        loadModules/<@node_modules/angular/angular.js:5023:15
        forEach@node_modules/angular/angular.js:408:11
        loadModules@node_modules/angular/angular.js:4983:5
        createInjector@node_modules/angular/angular.js:4900:19
        WorkFn@node_modules/angular-mocks/angular-mocks.js:3173:44
        <Jasmine>
        TypeError: $controller is not a function in app/parkinput/parkinput.spec.js (line 35)
        @app/parkinput/parkinput.spec.js:35:30
        <Jasmine>

`

再次,请原谅我的WoT,但我无能为力。如果需要,我可以提供karma配置文件。提前感谢您的帮助。

0 个答案:

没有答案