gpt4 book ai didi

angular - 尝试运行 "ng test"时没有 MatSnackBar 错误的提供者

转载 作者:太空狗 更新时间:2023-10-29 17:30:55 24 4
gpt4 key购买 nike

所以我正在尝试在我的 Angular 4 Angular CLI 上运行“ng test”。我有很多问题,例如如果测试失败,UI 不会加载任何有用的消息,但幸运的是控制台可以工作。无论如何,出现此错误表明我没有在规范中为 MatSnackBar 提供提供程序,但是当我这样做时,我得到了一个不同的错误。

Chrome 66.0.3359 (Linux 0.0.0) BranchNameCellComponent should create FAILED
Error: No provider for MatSnackBar!

导入 MatSnackBar 时出错

Chrome 66.0.3359 (Linux 0.0.0) BranchNameCellComponent should create FAILED
Failed: Unexpected value 'MatSnackBar' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.

在我的组件代码中,我正在导入 MatSnackBar 并在构造函数中调用它。它工作正常。

import { MatSnackBar } from '@angular/material';
....
constructor(private httpClient: HttpClient, private fb: FormBuilder, public snackBar: MatSnackBar) { }

但在规范中,我尝试这样做无济于事。

import { MatSnackBar } from '@angular/material';
....
describe('BranchNameCellComponent', () => {
let component: BranchNameCellComponent;
let fixture: ComponentFixture<BranchNameCellComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ BranchNameCellComponent ],
imports: [ HttpClientTestingModule, ReactiveFormsModule, MatSnackBar ],
schemas: [ NO_ERRORS_SCHEMA ]
})
.compileComponents();
}));

it('should create', () => {
fixture = TestBed.createComponent(BranchNameCellComponent);
component = fixture.componentInstance;
expect(component).toBeTruthy();
});
});

按照 Diego 的建议导入模块后,我现在在同一测试中遇到超时错误

08 06 2018 16:14:52.839:WARN [Chrome 66.0.3359 (Linux 0.0.0)]: Disconnected (1 times), because no message in 10000 ms.
Chrome 66.0.3359 (Linux 0.0.0) ERROR
Chrome 66.0.3359 (Linux 0.0.0) ERROR
Disconnected, because no message in 10000 ms.
Chrome 66.0.3359 (Linux 0.0.0): Executed 16 of 56 DISCONNECTED (11.204 secs / 1.241 secs)
Chrome 66.0.3359 (Linux 0.0.0) ERROR
Chrome 66.0.3359 (Linux 0.0.0): Executed 16 of 56 DISCONNECTED (11.204 secs / 1.241 secs)

最佳答案

您应该导入模块而不是组件。

import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
....
describe('BranchNameCellComponent', () => {
let component: BranchNameCellComponent;
let fixture: ComponentFixture<BranchNameCellComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ BranchNameCellComponent ],
imports: [ HttpClientTestingModule, ReactiveFormsModule, MatSnackBarModule ],
schemas: [ NO_ERRORS_SCHEMA ]
}).compileComponents();
}));

it('should create', () => {
fixture = TestBed.createComponent(BranchNameCellComponent);
component = fixture.componentInstance;
expect(component).toBeTruthy();
});
});

对于与超时相关的问题,请尝试将其添加到您的 karma 配置文件中:

captureTimeout: 210000,
browserDisconnectTolerance: 3,
browserDisconnectTimeout : 210000,
browserNoActivityTimeout : 210000,

关于angular - 尝试运行 "ng test"时没有 MatSnackBar 错误的提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50768318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com