Unit testing 角度2 RC4-无法解析';路由器&x27;
我已经为我的组件编写了一个测试,但它没有通过 错误:无法解析“路由器”的所有参数(?, ?). 确保所有参数都用Inject或 具有有效的类型批注,并且“路由器”用 可注射的 非常简单的组件,它所做的一切就是从一个使用Http调用的服务中获取国家,并在另一个服务(也是Http调用)上调用save方法。我用我的模拟类模拟这些服务。下面是我的测试代码Unit testing 角度2 RC4-无法解析';路由器&x27;,unit-testing,typescript,angular,mocking,Unit Testing,Typescript,Angular,Mocking,我已经为我的组件编写了一个测试,但它没有通过 错误:无法解析“路由器”的所有参数(?, ?). 确保所有参数都用Inject或 具有有效的类型批注,并且“路由器”用 可注射的 非常简单的组件,它所做的一切就是从一个使用Http调用的服务中获取国家,并在另一个服务(也是Http调用)上调用save方法。我用我的模拟类模拟这些服务。下面是我的测试代码 import { By } from '@angular/platform-browser'; import { DebugEle
import { By } from '@angular/platform-browser';
import { DebugElement, provide } from '@angular/core';
import { disableDeprecatedForms, provideForms } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import * as Rx from 'rxjs/Rx';
import {
beforeEach, beforeEachProviders,
describe, xdescribe,
expect, it, xit,
async, inject, addProviders,
TestComponentBuilder, ComponentFixture
} from '@angular/core/testing';
import { SupplierFormComponent } from './supplier-form.component';
import { SupplierService } from './supplier.service';
import { AppService } from '../shared/app.service';
describe('Component: Supplier', () => {
var builder;
beforeEachProviders(() => {
return [
disableDeprecatedForms(),
provideForms(),
Router, ActivatedRoute,
provide(AppService, { useClass: MockAppService }),
provide(SupplierService, { useClass: MockSupplierService })
];
});
beforeEach(inject([TestComponentBuilder], (tcb) => {
builder = tcb;
}));
it('should create Supplier Component', async(() => {
/*.overrideProviders(
SupplierFormComponent,
[{ provide: AppService, useClass: MockAppService }]
)*/
builder.createAsync(SupplierFormComponent)
.then((fixture: ComponentFixture<SupplierFormComponent>) => {
fixture.detectChanges
var compiled = fixture.debugElement.nativeElement;
console.log(compiled);
})
.catch((error) => {
console.log("error occured: " + error);
});
}));
});
class MockAppService {
public name = "Injected App Service";
public fakeResponse: any = [{ "id": 1, "name": "uk" }];
public getCountry() {
return this.fakeResponse;
}
}
class MockSupplierService {
public name = "Injected Supplier Service";
saveSupplier(supplier: any): boolean {
return true;
}
}
从'@angular/platform browser'导入{By};
从'@angular/core'导入{DebugElement,提供};
从'@angular/forms'导入{disableDeprecatedForms,provideForms};
从'@angular/Router'导入{Router,ActivatedRoute};
从“rxjs/Rx”导入*作为Rx;
进口{
在每个之前,在每个提供者之前,
描述,描述,
期待,它,退出,,
异步、注入、添加提供程序,
TestComponentBuilder,ComponentFixture
}来自“角度/核心/测试”;
从“./supplier form.component”导入{SupplierFormComponent};
从“/supplier.service”导入{SupplierService};
从“../shared/app.service”导入{AppService};
描述('组件:供应商',()=>{
var生成器;
在每个提供程序之前(()=>{
返回[
disableDeprecatedForms(),
provideForms(),
路由器,激活的路由,
提供(AppService,{useClass:MockAppService}),
提供(SupplierService,{useClass:MockSupplierService})
];
});
每次之前(注入([TestComponentBuilder],(tcb)=>{
builder=tcb;
}));
它('应该创建供应商组件'),异步(()=>{
/*.覆盖提供程序(
供应商表单组件,
[{提供:AppService,useClass:MockAppService}]
)*/
createAsync(SupplierFormComponent)
.然后((夹具:组件夹具)=>{
fixture.detecttchanges
编译变量=fixture.debugElement.nativeElement;
console.log(编译);
})
.catch((错误)=>{
日志(“发生错误:+错误”);
});
}));
});
类MockAppService{
public name=“注入应用服务”;
公共伪造响应:any=[{“id”:1,“name”:“uk”}];
公营国(){
返回此.fakeResponse;
}
}
类供应商服务{
public name=“供应商服务”;
保存供应商(供应商:任意):布尔值{
返回true;
}
}
你知道如何用RC.4正确模拟路由器吗。我也有同样的问题。你找到解决方案了吗?没有,还没有:(这个问题已经问了将近5天了,没有回答。您好!测试API有很多变化(在每个Providers和AddProviders消失之前)。我建议您在RC5之后再试一次。请阅读发行说明,并在Angular 2 repo中专门测试路由器。我曾尝试为RC4做一些工作,但需要一些特定的模拟。现在应该对其进行排序,但还没有时间查看详细信息。@GerardSans感谢您的输入。我将更新到RC5,看看它是否会首先破坏我的网站,并将尝试测试。星期二周会上见:)这将有助于我解决同样的问题。你找到解决方案了吗?没有,还没有:(这个问题已经问了将近5天了,没有回答。您好!测试API有很多变化(在每个Providers和AddProviders消失之前)。我建议您在RC5之后再试一次。请阅读发行说明,并在Angular 2 repo中专门测试路由器。我曾尝试为RC4做一些工作,但需要一些特定的模拟。现在应该对其进行排序,但还没有时间查看详细信息。@GerardSans感谢您的输入。我将更新到RC5,看看它是否会首先破坏我的网站,并将尝试测试。星期二周会上见:)这将有所帮助
import { By } from '@angular/platform-browser';
import { DebugElement, provide } from '@angular/core';
import { disableDeprecatedForms, provideForms } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import * as Rx from 'rxjs/Rx';
import {
beforeEach, beforeEachProviders,
describe, xdescribe,
expect, it, xit,
async, inject, addProviders,
TestComponentBuilder, ComponentFixture
} from '@angular/core/testing';
import { SupplierFormComponent } from './supplier-form.component';
import { SupplierService } from './supplier.service';
import { AppService } from '../shared/app.service';
describe('Component: Supplier', () => {
var builder;
beforeEachProviders(() => {
return [
disableDeprecatedForms(),
provideForms(),
Router, ActivatedRoute,
provide(AppService, { useClass: MockAppService }),
provide(SupplierService, { useClass: MockSupplierService })
];
});
beforeEach(inject([TestComponentBuilder], (tcb) => {
builder = tcb;
}));
it('should create Supplier Component', async(() => {
/*.overrideProviders(
SupplierFormComponent,
[{ provide: AppService, useClass: MockAppService }]
)*/
builder.createAsync(SupplierFormComponent)
.then((fixture: ComponentFixture<SupplierFormComponent>) => {
fixture.detectChanges
var compiled = fixture.debugElement.nativeElement;
console.log(compiled);
})
.catch((error) => {
console.log("error occured: " + error);
});
}));
});
class MockAppService {
public name = "Injected App Service";
public fakeResponse: any = [{ "id": 1, "name": "uk" }];
public getCountry() {
return this.fakeResponse;
}
}
class MockSupplierService {
public name = "Injected Supplier Service";
saveSupplier(supplier: any): boolean {
return true;
}
}