2013-02-07 17 views
16

I spróbuj przetestować poniższy kod:Testy w angularjs: Błąd referencyjny funkcji injekcji

describe('myService test', function() { 
    describe('when I call myService.one', function() { 
     beforeEach(angular.module('TargetMarketServices')); 
     it('returns 1', inject(function (imagesRepository) { 
      expect(true).toEqual(true); 
     })); 

    }); 

}); 

Gdy ten kod jest wykonywany otrzymuję ten błąd:

TypeError: 'undefined' is not a function (evaluating 'this.func.apply(this.spec)') 
    at http://localhost:8080/testacular.js:76 
    at http://localhost:8080/context.html:35 
ReferenceError: Can't find variable: inject 
    at /home/peter/Dropbox/AngularJS/set-component/test/sets/sets-ihm.js:6 
    at /home/peter/Dropbox/AngularJS/set-component/test/sets/sets-ihm.js:8 
    at /home/peter/Dropbox/AngularJS/set-component/test/sets/sets-ihm.js:10 

PhantomJS 1.8: Zrealizowane 1 z 3 (1 FAILED) (pomijany 2) (0,072 s/0,01 s)

Do mojego testu używam Testacular z Jasmine i PhantomJS.

Odpowiedz

13

Linia gdzie trzeba

beforeEach(angular.module('TargetMarketServices')); 

powinny być

beforeEach(module('TargetMarketServices')); 

Jeśli spojrzeć na projekt w test/unit angular-phonecat/directivesSpec.js używa

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

Jeśli zmodyfikuję go, aby zamiast niego użyć angular.module:

beforeEach(angular.module('myApp.directives')); 

następnie uzyskać ten błąd podczas uruchamiania testacular również:

TypeError: 'undefined' is not a function (evaluating 'this.func.apply(this.spec)') 
+3

Ale kiedy używam tylko modułu, pojawia się błąd "ReferenceError: Can not find variable: module" – ChriX

+0

Przepraszam, miałem na myśli [kątowe-nasienie] (https://github.com/angular/angular-seed) wcześniej, a nie na kanciastą fonekat. Otrzymujesz ReferenceError, zakładam, ponieważ nie masz [angular-mocks.js] (https://github.com/angular/angular-seed/blob/master/test/lib/angular/ angular-mocks.js) w twoim testowym pliku konfiguracji testowania jednostkowego. –

+1

Problem został rozwiązany, biorąc plik kątowy-mocks.js. – ChriX

26

angularjs zapewnia dwie biblioteki Testowanie:

  • kątowe-mocks.js
  • skośnych scenario.js

Kątowe mocks jest używany do testowania Jasmine i Karma sol. Publikuje globalne metody module() i inject(), które będą używane w testach Jasmine. Oznacza to, że musisz załadować skrypt angular-mocks.js (po wczytaniu biblioteki/skryptu angular.js).

scenariusz kątowy jest używany tylko do testowania e2e.

Powiązane problemy