在 TestBed 中导入独立组件不起作用

问题描述 投票:0回答:1

我正在将我的应用程序从 Angular 17 升级到 Angular 19 (19.0.6) 和 nx20。我在 TestBed 和独立组件方面遇到了一些问题。

我有以下代码:

@Component({
  templateUrl: './connection.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: false
})
export class ConnectionComponent implements OnInit {...
beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [FakesModule, CoreTranslationTestingModule, MockComponent(FeatureConsentsComponent)],
      declarations: [
        ConnectionComponent,
        MockComponent(ButtonComponent),
        MockComponents(MotivationBlockComponent, MotivationBlockItemComponent)
      ],
      providers: [
        provideMockStore(),
        provideDummyTokens(),
      ]
    });
    fixture = TestBed.createComponent(ConnectionComponent);
    component = fixture.componentInstance;
  });

FeatureConsentsComponent 是一个独立组件:

@Component({
    selector: 'consents',
    templateUrl: './consents.component.html',
    changeDetection: ChangeDetectionStrategy.OnPush,
    imports: [NgIf, CheckBoxModule, LayoutModule, NgFor, AsyncPipe, TranslateModule],
    providers: [ConsentsHttpService, addDynamicComponentProviders(ConsentsErrorsProvider)]
})

但是当我尝试运行测试文件时出现以下错误:

    Unexpected "MockOfFeatureConsentsComponent" found in the "declarations" array of the "TestBed.configureTestingModule" call, "MockOfFeatureConsentsComponent" is marked as standalone and can't be declared in any NgModule - did you intend to import it instead (by adding it to the "imports" array)?

如果我在FeatureConsentsComponent上强制使用

standalone: true
,它就会起作用。 为什么?

angular angular-test angular-standalone-components
1个回答
0
投票

我想你使用了该库

ng-mocks

您描述的问题正是此问题之一:https://github.com/help-me-mom/ng-mocks/issues/10632

ng-mocks
升级到 14.13.2 应该可以解决您的问题

© www.soinside.com 2019 - 2024. All rights reserved.