javascript - 没有平台提供商

标签 javascript angular angular-material 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();
})

});

最佳答案

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

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

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

import { Platform } from '@angular/cdk/platform';

...

providers: [
  {provide: OdmService, useValue: odmServiceSub}, 
  UserNotificationService, 
  MatSnackBar, 
  OVERLAY_PROVIDERS, 
  ScrollStrategyOptions,
  ScrollDispatcher,
  Platform
]

关于javascript - 没有平台提供商,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47345175/

相关文章:

javascript - 弹出菜单打开新选项卡,而不是在同一选项卡中继续

angular - 在 Angular2 中使用 jQuery 插件

angular - 是否可以在 Jasmine 测试中获取模板引用变量?

angular - 使用 Mat-Paginator 时无法读取未定义的属性 'pipe'

angular - CdkVirtualScrollViewport 已附加(错误)

javascript - javascript中的RTP RTSP实现

javascript - encodeURIComponent() 忽略换行符

javascript - 使用 $parse 解析包含点字符的字段名称

javascript - 通过 id 获取元素 - Angular2

angular - 使用 Angular Material 实现 Notched Outline 主题时遇到问题