我想測試具有一些依賴性的簡單組件。所以我必須提供一些提供者。Angular 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.
我缺少的東西OIN的beforeEachProviders
功能?
注意:這個問題只與Angular 2的Jasmine單元測試有關。我不是在搜索相關的自舉應用程序的信息,因爲這已經在我的應用程序中確定,並且還有其他相關的問題在這裏。
@Sunil嗨,在這種情況下,我得到: '錯誤:RequestOptions沒有提供者! (AuthHttp - > RequestOptions)' –
@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)。 –
我不注射其他地方,這是奇怪的事情:/ –