![](/img/trans.png)
[英]Angular 6 - how to mock router.events url response in unit test
[英]angular unit test - how to mock router.config.data
所以我的組件中有以下代碼:
constructor(private router: Router){}
ngOnInit(){
module = this.router.config[0].data['env'].module
}
現在,當使用 Jasmine 和 Karma 運行我的單元測試時,我收到以下錯誤:
Cannot read property 'env' of undefined
基本上, config.data.env 是一個環境變量,它來自另一個組件到我的 libs 文件夾中的組件。 所以我需要模擬環境變量。
這就是我在我的規范文件中嘗試的:
const fakeData = {
data: {
env: {
module: 'myModule',
}
},
}
並在 TestBed.configureTestingModule 導入:
imports: [
RouterTestingModule.withRoutes(routes, fakeData),
]
withRoutes 上的第二個參數是 extraOptions,但它不起作用。
我如何模擬環境變量並將其設置在 router.config 上? 謝謝!
在routes
變量中,將 data 屬性附加到第一個元素並從RouterTestingModule.withRoutes(routes, fakeData)
中刪除fakeData
。
像這樣的東西:
const routes = [{
path: 'xyz',
component: YourComponent,
data: {
env: {
module: 'mockModule'
}
}
}];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.