没有平台提供商

red*_*der 0 javascript angular-material angular angular-cdk

所以我在为 Angular 4 应用程序进行单元测试时遇到了这个问题

发生的情况是它不断给出问题标题中所述的错误。

我尝试用谷歌搜索它,尝试导入一大堆不同的模块,最后发现这个“平台”的接近答案可能是@angular/browser平台中的 browserModule 。

因此,在我的单元测试中,我尝试导入它并声明它,但没有帮助。

任何人都可以帮忙解决这个问题,因为我什至不确定这个“平台”是什么?

问题:错误中的这个“平台”到底是什么以及如何修复它?

谢谢。

我已附上我的代码如下:

import { ComponentFixture, TestBed, async} from "@angular/core/testing";
import { DebugElement, CUSTOM_ELEMENTS_SCHEMA, PlatformRef} from 
"@angular/core";
import { TeamCreationAssignmentComponent } from "./team-creation-assignment.component";
import { OdmService } from "../../services/odm/odm.service";
import { UserNotificationService } from "../../services/user/user-notification.service";
import { MatSnackBar } from "@angular/material";
import { OVERLAY_PROVIDERS, ScrollStrategyOptions, ScrollDispatcher} from "@angular/cdk/overlay";

describe('Team creation assignment component', () => {
let comp: TeamCreationAssignmentComponent;
let fixture: ComponentFixture<TeamCreationAssignmentComponent>;

let odmServiceSub = {};


beforeEach(async(() => {
    TestBed.configureTestingModule({

        declarations: [TeamCreationAssignmentComponent],
        //imports: [BrowserModule],
        schemas: [CUSTOM_ELEMENTS_SCHEMA],
        providers: [
            {provide: OdmService, useValue: odmServiceSub}, 
            UserNotificationService, 
            MatSnackBar, 
            OVERLAY_PROVIDERS, 
            ScrollStrategyOptions,
            ScrollDispatcher,
        ],
    })
    .compileComponents();
}));

beforeEach(() => {
    fixture = TestBed.createComponent(TeamCreationAssignmentComponent);
    comp = fixture.componentInstance;
});

it('should have defined component', () => {
    expect(comp).toBeDefined();
})
Run Code Online (Sandbox Code Playgroud)

});

Zaa*_*hid 5

我使用 FocusMonitor 遇到了类似的问题(在 Angular 5 中),并想测试依赖于它的组件。FocusMonitor 又具有 Platform 的依赖性。

在您的情况下,它与ScrollDispatcher具有相同的依赖关系。

您需要在 TestBed 的提供程序中添加平台

import { Platform } from '@angular/cdk/platform';
Run Code Online (Sandbox Code Playgroud)

...

providers: [
  {provide: OdmService, useValue: odmServiceSub}, 
  UserNotificationService, 
  MatSnackBar, 
  OVERLAY_PROVIDERS, 
  ScrollStrategyOptions,
  ScrollDispatcher,
  Platform
]
Run Code Online (Sandbox Code Playgroud)