尝试运行“ ng测试”时,没有提供MatSnackBar错误的提供程序

Coc*_*ett 9 testing angular-material angular-cli angular

因此,我试图在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!
Run Code Online (Sandbox Code Playgroud)

当我包含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.
Run Code Online (Sandbox Code Playgroud)

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

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

但是在规范中,我没有用。

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();
  });
});
Run Code Online (Sandbox Code Playgroud)

按照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)
Run Code Online (Sandbox Code Playgroud)

小智 26

这件事最近发生在我身上,我决定是这样的::

包含文件aap.module.ts

import { MatSnackBarModule } from '@angular/material/snack-bar';
...
...
imports: [
    ...
    MatSnackBarModule
  ]
...
Run Code Online (Sandbox Code Playgroud)

关于文件组件或服务:

import { MatSnackBar } from '@angular/material/snack-bar';
...
...
constructor(private snackBar: MatSnackBar) { }
...
Run Code Online (Sandbox Code Playgroud)


dlc*_*ozo 13

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

import { MatSnackBar, MatSnackBarModule } from '@angular/material';
....
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();
  });
});
Run Code Online (Sandbox Code Playgroud)

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

captureTimeout: 210000,
browserDisconnectTolerance: 3, 
browserDisconnectTimeout : 210000,
browserNoActivityTimeout : 210000,
Run Code Online (Sandbox Code Playgroud)


Awa*_*sir 10

当我在我的角度应用程序中导航到某个延迟加载的模块时,我遇到了这个错误。该模块正在导入,但我也MatSnackBarModule需要导入MatSnackBarModule文件app.module.ts