[英]No provider for Platform
So I have come across this issue making a unit test for an Angular 4 application 所以我遇到了这个问题,对Angular 4应用程序进行了单元测试
What happens is that it keeps giving the error stated here in the question title. 发生的情况是,它始终给出问题标题中所述的错误。
I tried to google it, tried to import a whole bunch of different modules and finally found that a close answer to what this "Platform" is might be the browserModule from @angular/browser
Platform. 我试图用Google搜索它,试图导入一大堆不同的模块,最后发现对这个“平台”的答案很可能是
@angular/browser
Platform的browserModule。
So in my unit testing, I tried to import it and declare it but it did not help. 因此,在单元测试中,我尝试导入它并声明它,但没有帮助。
Can anyone please help with this as I'm not even sure what this "Platform" is? 任何人都可以帮忙,因为我什至不确定这个“平台”是什么?
Question: what is exactly this "Platform" in the error and how to fix it? 问题:错误中的此“平台”究竟是什么,以及如何解决?
Thanks. 谢谢。
I have attached my code as below: 我已附上我的代码,如下所示:
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();
})
}); });
I ran into a similar problem (in Angular 5) using FocusMonitor and wanting to test the component depending on it. 我在使用FocusMonitor遇到了一个类似的问题(在Angular 5中),并想根据它测试组件。 FocusMonitor in turn has a dependency of Platform.
反过来,FocusMonitor具有平台依赖性。
In your case it's the same dependency with ScrollDispatcher . 在您的情况下,它与ScrollDispatcher的依赖项相同。
You'll need to add Platform in your TestBed's providers 您需要在TestBed的提供商中添加平台
import { Platform } from '@angular/cdk/platform';
... ...
providers: [
{provide: OdmService, useValue: odmServiceSub},
UserNotificationService,
MatSnackBar,
OVERLAY_PROVIDERS,
ScrollStrategyOptions,
ScrollDispatcher,
Platform
]
Whenever you get a message that there is "No provider for XXX" it generally means that you are missing something from the providers
array in the configureTestingModule
method. 每当您收到一条消息,指出“ XXX都没有提供程序”时,通常意味着您在
configureTestingModule
方法中的providers
数组中缺少某些内容。 Have you tried adding PlatformRef
to the providers
array? 您是否尝试过将
PlatformRef
添加到providers
数组中? Like this: 像这样:
providers: [
{provide: OdmService, useValue: odmServiceSub},
UserNotificationService,
MatSnackBar,
OVERLAY_PROVIDERS,
ScrollStrategyOptions,
ScrollDispatcher,
PlatformRef // <- added here
],
One thing you are missing however is a call to detectChanges
in your second beforeEach
, it should look like this: 然而,你缺少的一件事是将呼叫
detectChanges
在你的第二个beforeEach
,它应该是这样的:
beforeEach(() => {
fixture = TestBed.createComponent(TeamCreationAssignmentComponent);
comp = fixture.componentInstance;
fixture.detectChanges(); // <- this is required
});
One thing I will say is that in my Angular 4 app, there are around 800 unit tests, and not a single one of them uses or requires this PlatformRef
. 我要说的一件事是,在我的Angular 4应用中,大约有800个单元测试,其中没有一个使用或要求使用
PlatformRef
。 I think the issue is the missing detectChanges
rather than anything to do with this "platform". 我认为问题是缺少的
detectChanges
而不是与此“平台”有关的任何东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.