2017-05-17 56 views
1

當我做角度單元測試時,發現了這個錯誤你的一些測試做了整頁重新加載

ALERT:'Add Success!' Chrome 58.0.3029(Windows 10 0.0.0)錯誤 一些測試做了整頁重新加載! 鉻58.0.3029(視窗10 0.0.0):執行的1個ERROR 0(0.503秒/ 0秒)

// strategyAdd.component.ts 
 
export class StrategyAdd{ 
 

 
\t strategy = new Strategy(); 
 
    status:string; 
 
    iscookies = ['','Yes','No']; 
 
    isuseragents = ['','Yes','No']; 
 
    constructor(private strategyService: StrategyTablesService, 
 
    \t \t \t \t \t \t private router:Router, 
 
    \t \t \t  \t \t private location: Location 
 
) { } 
 

 

 
    onClickCreate(strategy:Strategy):void { 
 
    strategy.starttime = this.getDate(); 
 
    \t this.strategyService.createStrategy(strategy).subscribe((data) => { 
 
    \t \t this.status = data.json().status; 
 
    \t \t if(this.status=="succeed"){ 
 
    \t \t \t alert("Add Success!"); 
 
    \t \t \t location.reload(); 
 
    \t \t }else{ 
 
    \t \t \t alert("Add failed!"); 
 
    \t \t } 
 
    \t }, 
 
    \t 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

你做位置.reload()並使用真實位置 - 因此它將重新加載頁面。嘗試從提供者數組中刪除位置,因爲RouterTestingModule已經提供了一個存根位置。

相關問題