错误:[$ injector:modulerr] angularjs 1.7和jasmine

问题描述 投票:1回答:2

controller.js

  var testApp = angular.module('testApp', ['testModule']);

  angular.module('testModule')
    .component('testModule', {
      template: '<p> some things will be here</p>',
      controller: [
        '$scope', '$http', 
        function testController($scope, $http){
          $scope.z = 0;
          $scope.sum = function() {
            $scope.z = $scope.x + $scope.y;
          };
        }
      ]
    });

test.js

describe('testModule', function () {

    beforeEach(module('testApp'));

    var $controller;

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

    describe('tests', function () {

          // directive test i




          // controller test
          it('1 + 2 should equal 3', function () {
              var $scope = {};
              var controller = $controller('testController', { $scope: $scope });
              $scope.x = 1;
              $scope.y = 2;
              $scope.sum();
              expect($scope.z).toBe(3);
          });   

          // service  test
          it('http get test', inject(function($http, $httpBackend){
            var $scope = {};
            var controller = $controller('testController', { $scope: $scope });
            $http.get('http://localhost/ibrahim')
              .success(function(data, status, headers, config){
                $scope.valid = true;
                $scope.response = data;
              })
              .error(function(data, status, headers, config){
                $scope.vaid = false;
              });

              $httpBackend
                .when('GET', 'http://localhost/ibrahim')
                .respond(200, {
                    name: 'ibrahim',
                    country: 'sweden'
                });

              $httpBackend.flush();

              expect($scope.valid).toBe(true);
              expect($scope.response).toEqual({
                name: 'ibrahim',
                country: 'sweden'
              });
          }));

      }
    );
  });

我收到了这个错误

   Error: [$injector:modulerr] http://errors.angularjs.org/1.4.0-rc.2/$injector/modulerr?p0=testApp&p1=Error%3A%20%5B%24injector%3Amodulerr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.4.0-rc.2%2F%24injector%2Fmodulerr%3Fp0%3DtestModule%26p1%3DError%253A%2520%255B%2524injector%253Anomod%255D%2520http%253A%252F%252Ferrors.angularjs.org%252F1.4.0-rc.2%252F%2524injector%252Fnomod%253Fp0%253DtestModule%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A6%253A421%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A23%253A384%250A%2520%2520%2520%2520at%2520a%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A23%253A25)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A23%253A268%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A37%253A202%250A%2520%2520%2520%2520at%2520n%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A7%253A322)%250A%2520%2520%2520%2520at%2520g%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A37%253A50)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A37%253A219%250A%2520%2520%2520%2520at%2520n%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A7%253A322)%250A%2520%2520%2520%2520at%2520g%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A37%253A50)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A6%3A421%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A37%3A476%0A%20%20%20%20at%20n%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A7%3A322)%0A%20%20%20%20at%20g%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A37%3A50)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A37%3A219%0A%20%20%20%20at%20n%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A7%3A322)%0A%20%20%20%20at%20g%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A37%3A50)%0A%20%20%20%20at%20Object.ab%20%5Bas%20injector%5D%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A40%3A332)%0A%20%20%20%20at%20UserContext.workFn%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular-mocks.js%3F71a3fefebedb617c8cd52b83f351d31a45417323%3A2409%3A56)%0A%20%20%20%20at%20QueueRunner.attempt%20(http%3A%2F%2Flocalhost%3A9876%2FC%3A%2FUsers%2Fibrahim.yazici%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F1f336da4025323945fc818c69bdc564a85081e4e%3A5462%3A44)
        at lib/angular.min.js:6:421
        at lib/angular.min.js:37:476
        at n (lib/angular.min.js:7:322)
        at g (lib/angular.min.js:37:50)
        at Object.ab [as injector] (lib/angular.min.js:40:332)
        at UserContext.workFn (lib/angular-mocks.js:2409:56)
        at <Jasmine>
    Error: [$injector:modulerr] http://errors.angularjs.org/1.4.0-rc.2/$injector/modulerr?p0=testApp&p1=Error%3A%20%5B%24injector%3Amodulerr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.4.0-rc.2%2F%24injector%2Fmodulerr%3Fp0%3DtestModule%26p1%3DError%253A%2520%255B%2524injector%253Anomod%255D%2520http%253A%252F%252Ferrors.angularjs.org%252F1.4.0-rc.2%252F%2524injector%252Fnomod%253Fp0%253DtestModule%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A6%253A421%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A23%253A384%250A%2520%2520%2520%2520at%2520a%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A23%253A25)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A23%253A268%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A37%253A202%250A%2520%2520%2520%2520at%2520n%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A7%253A322)%250A%2520%2520%2520%2520at%2520g%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A37%253A50)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A37%253A219%250A%2520%2520%2520%2520at%2520n%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A7%253A322)%250A%2520%2520%2520%2520at%2520g%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A37%253A50)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A6%3A421%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A37%3A476%0A%20%20%20%20at%20n%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A7%3A322)%0A%20%20%20%20at%20g%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A37%3A50)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A37%3A219%0A%20%20%20%20at%20n%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A7%3A322)%0A%20%20%20%20at%20g%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A37%3A50)%0A%20%20%20%20at%20Object.ab%20%5Bas%20injector%5D%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A40%3A332)%0A%20%20%20%20at%20UserContext.workFn%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular-mocks.js%3F71a3fefebedb617c8cd52b83f351d31a45417323%3A2409%3A56)%0A%20%20%20%20at%20QueueRunner.attempt%20(http%3A%2F%2Flocalhost%3A9876%2FC%3A%2FUsers%2Fibrahim.yazici%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F1f336da4025323945fc818c69bdc564a85081e4e%3A5462%3A44)
        at lib/angular.min.js:6:421
        at lib/angular.min.js:37:476
        at n (lib/angular.min.js:7:322)
        at g (lib/angular.min.js:37:50)
        at Object.ab [as injector] (lib/angular.min.js:40:332)
        at UserContext.workFn (lib/angular-mocks.js:2409:56)
        at <Jasmine>

Chrome 72.0.3626(Windows 10.0.0)错误{“message”:“在所有\ n未捕获错误后抛出错误:[$ injector:nomod] http://errors.angularjs.org/1.4.0-rc.2/ $ injector / nomod?p0 = testModule”,“str”:“An afterAll \ n未捕获错误引发错误:[$ injector:nomod] http://errors.angularjs.org/1.4.0-rc.2/ $ injector / nomod?p0 = testModule“}

如果我只使用这个控制器代码,我没有错误

  angular.module('testApp', [])
  .controller('testController', function testController($scope, $http) {
    $scope.z = 0;
    $scope.sum = function() {
      $scope.z = $scope.x + $scope.y;
    };
  });

但我也想测试指令。没有模板,我不明白我是怎么做的。为什么我会收到此错误,如何解决?或者有没有办法在不编写模板的情况下测试指令?谢谢

angularjs unit-testing jasmine karma-jasmine
2个回答
1
投票

试试这个

 var myApp = angular.module('testModule', [])

 myApp.component('testModule', {
 template: '<p> some things will be here</p>',
 controller: [
    '$scope', '$http', 
    function testController($scope, $http){
      $scope.z = 0;
      $scope.sum = function() {
        $scope.z = $scope.x + $scope.y;
      };
    }
  ]
});

var testApp = angular.module('testApp', ['testModule']);

这个对我有用


-1
投票

我相信你需要像这样注入$compile提供者:

var $compile, $controller;

...

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

// your test code

查看Testing Directives segment of the official guide了解更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.