2016-07-15 5 views
10

いくつかの依存関係を持つ単純なコンポーネントをテストします。だから私はいくつかのプロバイダを提供する必要があります。角2ユニットテストエラー: 'RequestOptions'のすべてのパラメータを解決できません

/* tslint:disable:no-unused-variable */ 

import { By }   from '@angular/platform-browser'; 
import { DebugElement, provide } from '@angular/core'; 

import { 
    beforeEach, 
    beforeEachProviders, 
    describe, 
    expect, 
    it, 
    inject, 
    fakeAsync, 
    TestComponentBuilder 
} from '@angular/core/testing'; 

import { AuthHttp, AuthConfig } from 'angular2-jwt'; 
import { Router, provideRouter } from '@angular/router'; 
import { Http, ConnectionBackend, RequestOptions, HTTP_PROVIDERS } from '@angular/http'; 
import { LogoutButtonComponent } from './logout-button.component'; 
import { UserService } from '../../services/index'; 

describe('Component: LogoutButtonComponent',() => { 

    let component: LogoutButtonComponent; 

    beforeEachProviders(() => [ 
    LogoutButtonComponent, 
    Http, 
    provide(AuthHttp, { useFactory: Http }), 
    provide(AuthConfig, { useValue: new AuthConfig() }), 
    ConnectionBackend, 
    RequestOptions, 
    UserService 
    ]); 

    beforeEach(inject([AuthHttp, UserService, LogoutButtonComponent], 
    (comp: LogoutButtonComponent) => { 
     component = comp; 
    })); 

    it('should inject UserService',() => { 
    // My test here 
    }); 

}); 

私は次のエラーを取得していますが:

Error: Cannot resolve all parameters for 'RequestOptions'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'RequestOptions' is decorated with Injectable.

私はbeforeEachProviders機能OIN何かが足りないのですか?

注:この質問はジャスミンを使用した角度2のユニットテストにのみ関連しています。私はinfos関連のブートストラップアプリを探しているわけではありません。これは既に私のアプリでは大丈夫ですが、ここに他の関連する質問があります。

答えて

4

RequestOptionsは注射可能ではありませんが、これをクラスに注入することはありません。代わりに、HTTPリクエストを行うときに、必要に応じてインスタンス化します。だから、beforeEachProvidersからそれを削除し、あなたが実際のテストでそれを必要とする場合beforeEachに1をインスタンス化することができます

let options: RequestOptions; 

beforeEach(inject([AuthHttp, UserService, LogoutButtonComponent], 
    (comp: LogoutButtonComponent) => { 
     component = comp; 
     options = new RequestOptions({method: RequestMethod.Post}); 
})); 
+0

こんにちは@Sunil、この場合には、私は取得しています: 'Error:RequestOptionsのプロバイダがありません! (AuthHttp - > RequestOptions) ' –

+0

@ VassilisPitsあなたはどこでそのエラーを取得していますか? 'RequestOptions'を注入しようとしているところで、あなたはそれをしてはいけません。 1)このクラスを挿入することはできません。 'Injectable()'アノテーションはありません。[source](https://github.com/angular/angular/blob/2.0.0-rc.4/modules/)の注釈を確認してください。 %40angular/http/src/base_request_options.ts#L17-L123)。上記のように 'new'演算子を持つ' RequestOptions'オブジェクトをインスタンス化する必要があります。また、[documentation](https://angular.io/docs/ts/latest/api/http/index/)に示されているように、 RequestOptions-class.html)。 –

+0

私は別の場所に注射していない、それは奇妙なことです:/ –

17

あなたのテストベッド構成にHttpModuleをインポートする必要があります。

import { HttpModule } from "@angular/http"; 

TestBed.configureTestingModule({ 
    imports: [ 
    HttpModule 
    ] 
}); 

そのユニットテストは

+0

あなたは私の日を救った...ありがとう。 –

+0

これは、beforeEachProvidersが廃止された最新のAngular 4で動作する唯一のソリューションです。 –

2

を動作するはずです後、私は@angular/httpからHttpModuleHttpをインポートして、私のエラーを修正しました:

import {HttpModule, Http} from "@angular/http"; 
... 

TestBed.configureTestingModule({ 
    imports: [HttpModule], // <!-- HTTP module 
    providers: [HttpService, SourceService, Http] // <!-- HTTP 
}); 
関連する問題