我有angular 2 webpack應用程序,所有webpack,根據angular.io webpack指南創建的karma配置。 我不使用aot。 我正在寫茉莉花單元測試規範來測試我的組件。 首先,我嘗試沒有異步塊,在這種情況下,單元測試只能得到執行,直到fixture.detectChanges()調用,之後的代碼不會被執行。看起來像fixture.detectChanges調用無限地被阻止。Angular 2單元測試 - 獲取錯誤無法加載'ng:///DynamicTestModule/module.ngfactory.js'
我嘗試通過在異步塊中包含代碼。 然後我得到以下錯誤。 錯誤:無法執行 '發送' 上 '的XMLHttpRequest':無法加載 'NG:/// DynamicTestModule /module.ngfactory.js'
代碼,而異步
beforeeach(()=> {
TestBed.configureTestingModule({
imports:[],
declaration :[Mycomp],
providers:[{ provide:MyService, useclass:MyMockService}]
});
fixture=TestBed.createComponent(Mycomp);
console.log(' before detect changes'):
fixture.detectChanges():
console.log('after detect changes');// this is not getting
logged .. karma shows 0 of 1 executed successfully
});
隨着異步
beforeeach(async(()=> {
TestBed.configureTestingModule({
imports:[],
declaration :[Mycomp],
providers:[{ provide:MyService, useclass:MyMockService}]
});
fixture=TestBed.createComponent(Mycomp);
fixture.detectChanges():
}));
得到錯誤無法加載dynamictestmodule/module.ngfactory.js
這是真的,但我也得到了同樣的錯誤,有時,當我有沒有正確嘲笑或服務如果我的組件試圖訪問其中一個服務返回的空值。仍然是一個有效的答案,但要注意,這可能並不總能解決問題。 – blo0p3r
剛剛發現,通過將'sourcemap'選項設置爲false可以得到更好的錯誤信息:'ng test --sourcemap = false' – blo0p3r
@ blo0p3r這是一個很好的結果!將sourcemap設置爲false會將無用的XMLHttpRequest錯誤轉換爲實際的根錯誤。 – newclearwinter