代码之家  ›  专栏  ›  技术社区  ›  Sarah

尝试运行“ng test”时没有matsnakbar错误的提供程序

  •  5
  • Sarah  · 技术社区  · 7 年前

    所以我试着在我的angular4angularcli上运行“ng测试”。我遇到了很多问题,比如如果测试失败,ui不会加载任何有用的消息,但幸运的是控制台工作正常。无论如何,这个错误是发生在说我没有给Matsnakbar一个规范中的提供者,但是当我这样做时,我得到了一个不同的错误。

    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)
    
    1 回复  |  直到 7 年前
        1
  •  11
  •   dlcardozo    7 年前

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

    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();
      });
    });
    

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

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