Unit testing 角度2单元测试错误:无法解析';请求选项';
我想测试一个具有一些依赖关系的简单组件。因此,除其他外,我必须提供一些供应商Unit testing 角度2单元测试错误:无法解析';请求选项';,unit-testing,typescript,angular,jasmine,Unit Testing,Typescript,Angular,Jasmine,我想测试一个具有一些依赖关系的简单组件。因此,除其他外,我必须提供一些供应商 /* tslint:disable:no-unused-variable */ import { By } from '@angular/platform-browser'; import { DebugElement, provide } from '@angular/core'; import { beforeEach, beforeEachProviders, describe
/* 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
});
});
尽管我得到了以下错误:
错误:无法解析“RequestOptions”(?)的所有参数。确保所有参数都用Inject修饰或具有有效的类型注释,并且“RequestOptions”用Injectable修饰
在每个提供程序之前的功能中是否缺少某些内容
注:此问题仅与Jasmine Angular 2的单元测试有关。我没有搜索infos relate bootstraping应用程序,因为这在我的应用程序中已经可以了,这里还有其他相关问题。RequestOptions
不是可注入的,您不能将其注入类中。相反,在发出HTTP请求时,可以根据需要实例化一个。因此,如果您在测试中确实需要它,您可以从每个提供程序之前的中删除它,并在每个提供程序之前的中实例化一个:
let options: RequestOptions;
beforeEach(inject([AuthHttp, UserService, LogoutButtonComponent],
(comp: LogoutButtonComponent) => {
component = comp;
options = new RequestOptions({method: RequestMethod.Post});
}));
您必须将HttpModule
导入到您的测试床配置中
import { HttpModule } from "@angular/http";
TestBed.configureTestingModule({
imports: [
HttpModule
]
});
在那之后,单元测试应该可以工作了我已经通过从@angular/Http
导入HttpModule
和Http
修复了我的错误:
import {HttpModule, Http} from "@angular/http";
...
TestBed.configureTestingModule({
imports: [HttpModule], // <!-- HTTP module
providers: [HttpService, SourceService, Http] // <!-- HTTP
});
从“@angular/Http”导入{HttpModule,Http}”;
...
TestBed.configureTestingModule({
imports:[HttpModule],//模拟用户服务可能更好,这样您就不必担心RequestOptions或HttpModule,下面是我解决上述问题的方法:
import { RouterTestingModule } from '@angular/router/testing';
import { TestBed } from '@angular/core/testing';
import { LogoutButtonComponent } from './logout-button.component';
import { UserService } from '../../services/index';
describe('Component: LogoutButtonComponent', () => {
let component: LogoutButtonComponent;
let fixture: ComponentFixture<LogoutButtonComponent>;
let mockUserService;
beforeEach(() => {
// Provide array of user service methods used in LogoutButtonComponent to the createSpyObj
mockUserService = jasmine.createSpyObj(['exampleUserServiceMethod']);
TestBed.configureTestingModule({
declarations: [ LogoutButtonComponent ],
providers: [
{ provide: UserService, useValue: mockUserService }
],
// Only if your component uses routing
imports: [
RouterTestingModule
]
});
fixture = TestBed.createComponent(LogoutButtonComponent);
component = fixture.componentInstance;
})
it('should inject UserService', () => {
// My test here
});
});
从'@angular/router/testing'导入{RouterTestingModule};
从“@angular/core/testing”导入{TestBed};
从“./logout button.component”导入{LogoutButtonComponent};
从“../../services/index”导入{UserService};
描述('Component:LogoutButtonComponent',()=>{
let组件:LogoutButtonComponent;
let夹具:组件夹具;
让我们一起享受服务;
在每个之前(()=>{
//向createSpyObj提供LogoutButtonComponent中使用的用户服务方法数组
mockUserService=jasmine.createSpyObj(['exampleUserServiceMethod']);
TestBed.configureTestingModule({
声明:[LogoutButtonComponent],
供应商:[
{提供:UserService,useValue:mockUserService}
],
//仅当组件使用路由时
进口:[
路由测试模块
]
});
fixture=TestBed.createComponent(LogoutButtonComponent);
组件=fixture.componentInstance;
})
它('应该注入用户服务',()=>{
//我的测试在这里
});
});
Hi@Sunil,在这种情况下,我得到:Error:RequestOptions没有提供程序!(AuthHttp->RequestOptions)
@VassilisPits你从哪里得到这个错误的?在某个地方你试图注入RequestOptions
,你不应该这样做。1)你不能注入这个类,它没有Injectable()
注释,检查。您只需要实例化RequestOptions
对象和new
操作符,如上所示,如中所示。我没有在其他地方注入,这是奇怪的事情:/这是唯一适用于最新Angular 4的解决方案,其中beforeachProviders
已被弃用预计起飞时间。