当我进行角度单位测试时,发现了这个错误
警告:'添加成功!' Chrome 58.0.3029(Windows 10 0.0.0)错误 你的一些测试做了整页重装! Chrome 58.0.3029(Windows 10 0.0.0):执行0 of 1 ERROR(0.503秒/ 0秒)
// strategyAdd.component.ts
export class StrategyAdd{
strategy = new Strategy();
status:string;
iscookies = ['','Yes','No'];
isuseragents = ['','Yes','No'];
constructor( private strategyService: StrategyTablesService,
private router:Router,
private location: Location
) { }
onClickCreate(strategy:Strategy):void {
strategy.starttime = this.getDate();
this.strategyService.createStrategy(strategy).subscribe((data) => {
this.status = data.json().status;
if(this.status=="succeed"){
alert("Add Success!");
location.reload();
}else{
alert("Add failed!");
}
},
error => console.log(error));
}
}

//// strategyAdd.component.spec.ts
class MockStrategyTablesService extends StrategyTablesService{
//noinspection JSAnnotator
createStrategy(strategy:Strategy){
var mockData={
"strategyid" : "12",
"status" : "succeed"
}
return Observable.of({
json:() => mockData
});
}
}
describe('override provide Service',()=>{
let comp;
let strategy = new Strategy();
beforeEach(()=>{
TestBed.configureTestingModule({
imports:[HttpModule,RouterTestingModule],
providers:[
StrategyAdd,
{provide:StrategyTablesService,useClass:MockStrategyTablesService},
//{provide:Router,useClass:RouterStub},
Location,
]
});
});
beforeEach(inject([StrategyAdd],s => {
comp = s;
}));
it('test onClickCreate',async(()=>{
comp.onClickCreate(strategy);
expect(comp.status).toEqual("success");
}));
});

答案 0 :(得分:1)
你做location.reload()并使用真正的位置 - 所以它会重新加载页面。尝试从providers数组中删除Location,因为RouterTestingModule已经提供了一个存根位置。