[英]Testing NgRx effects has stopped working after upgrade to Angular 15
我在 Angular 應用程序中使用 NgRx 效果。 升級到 Angular 15 后,測試停止並顯示以下錯誤消息:
Error: NG0202: This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid.
This can happen if the dependency type is a primitive like a string or if an ancestor of this class is missing an Angular decorator.
Please check that 1) the type for the parameter at index 0 is correct and 2) the correct Angular decorators are defined for this class and its ancestors.
但是被測的class是有注解的,構造函數參數類型正確。
被測class:
@Injectable()
export class ProductEffects {
constructor(private store$: Store<ProductState>, private actions$: Actions<ProductAction>, private productService: ProductService) {}
requestActiveProducts = createEffect(() =>
// ... effects
}
考試:
describe('ProductEffects', () => {
let storeSpy: Mocked<Store<ProductState>>;
let productServiceSpy: Mocked<ProductService>;
let actions$: Observable<ProductAction>;
let effects: ProductEffects;
beforeEach(() => {
storeSpy = createSpyObj('Store', ['select']);
productServiceSpy = createSpyObj('ProductService', [... ]);
TestBed.configureTestingModule({
providers: [
{ provide: Store, useValue: storeSpy },
{ provide: ProductService, useValue: productServiceSpy },
ProductEffects,
provideMockActions(() => actions$),
],
imports: [StoreModule.forRoot({})],
teardown: { destroyAfterEach: false },
});
effects = TestBed.inject(ProductEffects); // error occurs here
});
來自 package.json:
"@angular/core": "15.0.2",
"@ngrx/effects": "15.0.0"
我的錯。 我忘記更新devDependencies
中的@angular/cli
。
來自 v15 的NgRx 遷移指南:
版本 15 具有最低版本要求:
Angular 版本 15.x,Angular CLI 版本 15.x,TypeScript 版本 4.8.x RxJS 版本 ^6.5.3 || ^7.5.0
因此,按如下方式更新依賴項后,它起作用了:
"devDependencies": {
"@angular-devkit/build-angular": "15.0.2",
"@angular/cli": "15.0.2",
"@angular/compiler-cli": "15.0.2",
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.