Programing

각도 반환 모듈의 테스트 서비스가 정의되지 않았습니다

lottogame 2020. 7. 4. 10:37
반응형

각도 반환 모듈의 테스트 서비스가 정의되지 않았습니다


내 프로젝트 (GitHub의 Angular Seed 프로젝트에서 가져온)에서 기본 서비스 단위 테스트를 실행하려고하는데 "모듈이 정의되지 않았습니다"라는 오류가 계속 발생합니다.

참조 된 JavaScript 파일 의 순서와 관련이있을 수 있지만 작동하지 않는 것 같으므로 도움이 될 수 있기를 바랍니다.

테스트 구성은 다음과 같습니다.

basePath = '../';

파일 = [
''public / javascripts / lib / jquery-1.8.2.js ',
'public / javascripts / lib / angular.js ',
'public / javascripts / lib / angular- .js ',
'public / app.js ',
'public / controllers /
.js ',
'public / directives.js ',
'public / filters.js ',
'public / services.js ',
JASMINE,
JASMINE_ADAPTER,
'public / javascripts / lib / angular-mocks. js ',
'test / unit / *. js '];

자동 감시 = true;

브라우저 = [ 'Chrome'];

junitReporter = {outputFile : 'test_out / unit.xml', 제품군 : 'unit'};

서비스는 다음과 같습니다.

angular.module('myApp.services', []).
  value('version', '0.1');

테스트는 다음과 같습니다.

'use strict';

describe('service', function() {
  beforeEach(module('myApp.services'));


  describe('version', function() {
    it('should return current version', inject(function(version) {
      expect(version).toEqual('0.1');
    }));
  });
});

그리고 testacular를 통해 테스트를 실행할 때의 오류는 다음과 같습니다.

ReferenceError : 모듈이 정의되지 않았습니다


angular-mocks.js 파일 이 없습니다 .


나는 똑같은 문제가 있었고 왜 작동하지 않는지 이해했습니다 . angular-mocks.js 파일 전에 jasmine.js 자바 스크립트 를 참조해야 합니다. 실제로 angular-mocks.js는 Jasmine 이로 드되었는지 확인하고 모듈 기능이 창에 추가되는지 확인합니다.

Angular Mocks 코드 추출다음과 같습니다 .

(아래에있는 '해킹'에 대한 몇 가지 주석 후에 편집하십시오 : 이것은 코드의 추출 일뿐입니다.이 코드는 스스로 작성 해야하는 것이 아니며 이미 있습니다!)

window.jasmine && (function(window) {

[...]

  window.module = angular.mock.module = function() {
    var moduleFns = Array.prototype.slice.call(arguments, 0);
    return isSpecRunning() ? workFn() : workFn;
    /////////////////////
    [...]
  };

간단히 말해서 : angular-mocks.js 전에 jasmine.js 참조 하면됩니다.


The window.module function comes in angular-mocks.js and is a shorthand for angular.mock.module. As mentioned in the docs, the module function only works with Jasmine.

Using Testacular, the following example configuration file will load angular-mocks.js.

/** example testacular.conf.js */

basePath = '../';
files = [
  JASMINE,
  JASMINE_ADAPTER,
  'path/to/angular.js',
  'path/to/angular-mocks.js',   // for angular.mock.module and inject.
  'src/js/**/*.js',             // application sources
  'test/unit/**/*.spec.js'      // specs
];
autoWatch = true;
browsers = ['Chrome'];

And, as suggested elsewhere, you can run Testacular with debug logging to see what scripts are loaded (you can also see the same in the inspector):

testacular --log-level debug start config/testacular.conf.js

The angular.mock.inject docs include a pretty complete example.


We use 'module' without 'angular' in our unit tests and it works fine.

CoffeeScript:

describe 'DiscussionServicesSpec', ->
    beforeEach module 'DiscussionServices'
    beforeEach inject ... etc.

which compiles to

JavaScript:

describe('DiscussionServices', function() {
    beforeEach(module('DiscussionServices'));
    beforeEach(inject(function ... etc.

The only time I see something like the error you described is if in the testacular.conf.js file the angular-mocks.js file is not listed in the files section before the specs trying to use 'module'. If I put it after my tests in the 'files' list I get

ReferenceError: Can't find variable: module

(Our tests are being run through PhantomJS)


I had included angular-mocks.js in my karma config, but was still getting the error. It turns out the order is important in the files array. (duh) Just like in the head of an html doc, if a script calls angular before it's defined, and error occurs. So I just had to include my app.js after angular.js and angular-mocks.js.


If you're using Yeoman and its angular-generator, you probably get this error. Especially when you do the Tutorial ( ._.)
I fixed it, by copying the angular-mocks.js file, from the bower_components/angular-mocks dir to the test/mock dir. Of course you have to be sure, that your karma.conf.js file is configured correctly.
Greetings!


I had this same issue when I was doing something like var module = angular.module('my',[]). I needed to make sure it was surrounded by IIFE

참고URL : https://stackoverflow.com/questions/13334749/testing-service-in-angular-returns-module-is-not-defined

반응형