gpt4 book ai didi

angular - 你的一些测试做了一个完整的页面重新加载

转载 作者:行者123 更新时间:2023-11-28 20:38:29 29 4
gpt4 key购买 nike

当我做 Angular 单元测试时,发现了这个错误

警报:“添加成功!”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");
}));
});

最佳答案

您执行 location.reload() 并使用真实位置 - 因此它将重新加载页面。尝试从 providers 数组中删除 Location,因为 RouterTestingModule 已经提供了一个 stub Location。

关于angular - 你的一些测试做了一个完整的页面重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44013866/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com