2016-10-02 1 views
0

Ich teste meinen authService. Dies ist der vollständige Test, aber Karma sagt mir, authService ist undefiniert. Ich habe eine Menge Service, von dem AuthService abhängt, aber ich habe sie alle richtig injiziert und injiziert.Angular2 Service Unit-Test schlägt mit TypeError fehl: undefined ist kein Objekt

Error: Cannot resolve all parameters for 'AuthService'(BackendService, Store, LoggerService, undefined, ErrorService). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'AuthService' is decorated with Injectable. in /var/folders/zb/tpysrhsx7hbg1dnsn4gwtqq00000gn/T/8715f9a6c29e748f52c8f59e3e1daae3.browserify (line 34976)

authservice.spec.ts

import { provide } from "@angular/core"; 
import { AuthHttp } from "angular2-jwt"; 
import { HTTP_PROVIDERS, XHRBackend } from "@angular/http"; 
import { MockBackend } from "@angular/http/testing"; 
import { 
    TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, 
    TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS 
} from "@angular/platform-browser-dynamic/testing"; 
import { 
    beforeEachProviders, 
    inject, 
    beforeEach, 
    it, 
    describe, 
    setBaseTestProviders 
} from "@angular/core/testing"; 
import { Subject } from "rxjs/Subject"; 
import { AuthService } from "./auth.service"; 
import { BackendService } from "../../backend/backend.service"; 
import { ErrorService } from "../../error/error.service"; 
import { LoggerService } from "../../logger/logger.service"; 
import { NavService } from "../../nav/nav-service/nav.service"; 
import { Store } from "@ngrx/store"; 
import { TestComponentBuilder } from "@angular/compiler/testing"; 
import { ToastController, AlertController } from "ionic-angular"; 
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS); 

describe("AuthService",() => { 

    let response = new Subject(); 
    let tcb; 
    let authService; 
    let navService; 
    let backendService; 
    let errorService; 
    let store; 
    let loggerService; 

    class StubErrorService extends ErrorService { 
     constructor() { 
      super(null, null); 
     } 

     toast(title) { 
      console.error(title); 
     } 

     modal(title, subtitle) { 
      console.error(title, subtitle); 
     } 
    } 

    class StubBackendService extends BackendService { 

    } 

    class StubStore extends Store<any> { 

    } 

    class StubLoggerService extends LoggerService { 

    } 

    class StubNavService extends NavService { 

    } 

    // PROVIDE 

    beforeEachProviders(() => [ 
     HTTP_PROVIDERS, 
     provide(AuthHttp, { 
      useValue: { 
       get: (url: string) => { 
        return response; 
       } 
      } 
     }), 
     AuthService, 
     TestComponentBuilder, 
     provide(ToastController, {useClass: null}), 
     provide(AlertController, {useClass: null}), 
     provide(ErrorService, {useClass: StubErrorService}), 
     provide(XHRBackend, {useClass: MockBackend}), 
     provide(BackendService, {useClass: StubBackendService}), 
     provide(Store, {useClass: StubStore}), 
     provide(LoggerService, {useClass: StubLoggerService}), 
     provide(NavService, {useClass: StubNavService}) 
    ]); 

    // INJECTS 

    beforeEach(inject([TestComponentBuilder, AuthService, ErrorService, BackendService, Store, LoggerService, NavService], (_tcb, as, es, bs, s, ls, ns) => { 
     tcb = _tcb; 
     authService = as; 
     navService = ns; 
     errorService = es; 
     store = s; 
     backendService = bs; 
     loggerService = ls; 
    })); 

    it("should test authservice",() => { 
     authService.logout(); 
    }); 
}); 

Antwort

-1

Ich weiß nicht, seine falls mehr, nur zu sagen, dass ich ein fast identisches Problem hatte und ich beschlossen, es folgende official docs, wie man Testdienste. Ich hoffe es hilft!

+1

Link-Only-Antworten sind nicht nützlich, sobald die Verbindung abbricht. Bitte kopieren Sie den relevanten Teil der Lösung in die Antwort. – isherwood

+0

Eine technische Antwort nur zu sagen "lese die Dokumente" ist überhaupt nicht nützlich. Vor allem diese verlinkte Seite ergibt sich (Stand 2017-12-28) in einem 91-seitigen Word-Dokument _ (oder mindestens 77 Seiten, wenn ich direkt aus dem Web drucke und alle Seitennavigationen ausgeblendet habe) _ mit über 15.000 Wörtern. – Seika85

Verwandte Themen