【问题标题】:Angular - Material <mat-option> not visible in testsAngular - 材料 <mat-option> 在测试中不可见
【发布时间】:2020-03-07 00:09:22
【问题描述】:

我正在使用 Material 组件开发 Angular 9。我想在 mat-select 组件中显示一些值。

下面是app.component.html

<h2> Angular - Material </h2>

<mat-form-field>
  <mat-label>Type</mat-label>
  <mat-select >
    <mat-option value="string">Small text</mat-option>
    <mat-option value="number">Number</mat-option>
    <mat-option value="date">Date</mat-option>
  </mat-select>
</mat-form-field>

<hr>

我的 app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'angular-playground';
}

当我使用 ng serve 运行应用程序时,我可以看到包含所有选项的选择框。

但是当我尝试使用 karma ng test 测试这个组件时,我在选择框中没有看到任何选项。我尝试检查 HTML DOM,但在 select 中没有看到任何选项标签,控制台中也没有错误。

app.component.spec.ts

import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { MatFormFieldModule } from '@angular/material/form-field'; 
import {MatSelectModule} from '@angular/material/select'; 

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule, MatFormFieldModule, MatSelectModule
      ],
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });
});

这是一个非常基本的 Angular 应用程序,我有一个只有选择框的简单组件。不知道为什么它没有在测试中显示选项。

下面是我的 app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatFormFieldModule } from '@angular/material/form-field'; 
import {MatSelectModule} from '@angular/material/select'; 


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    MatFormFieldModule,
    MatSelectModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

请帮帮我。

【问题讨论】:

  • 您是否显示任何错误?我的猜测是,在尝试运行测试时,角度应该会报告一些错误。另外,不清楚AppMaterialModule是什么,请添加它的代码。这里我的猜测是它不包括 MatFormFieldModule、MatSelectModule 或 AppComponent 所需的其他东西。顺便说一句,无论如何我都会模拟所有这些东西,而不是使用真正的材料组件。
  • AppMaterialModule 只有 MatFormFieldModule、MatSelectModule 模块。请检查我更新的测试文件。为了简单起见,我用 MatFormFieldModule 和 MatselectModule 替换了 AppMaterialModule,请检查新的屏幕截图,控制台中没有错误。以及如何模拟这些组件?你能提供任何教程参考吗?
  • 我可能在这里没有得到任何东西。您到底期望什么不按您认为的方式工作?你希望你的测试测试什么?现在您的测试只是检查您的组件可以成功实例化的事实,并且由于它确实通过了它只是意味着您正确设置了所有依赖项。如果您希望您的测试做更多的事情 - 那么,您需要将它放入测试中......但究竟是什么?
  • 在关闭选择之前,选项不在 DOM 中。要显示选项,您应该打开选择。为此,请在组件中将 mat-select 作为视图子项,然后在测试调用中打开选择。请注意,默认情况下打开选择具有动画效果,因此您可能需要将测试设为假异步并在尝试打开选择后调用滴答声并检测更改

标签: angular angular-material


【解决方案1】:

您必须触发 MatSelectComponent 的切换来显示您的选项列表,如下所示:

const matSelectComponent = fixture.debugElement.query(By.directive(MatSelect)).componentInstance;
fixture.detectChanges();

现在您可以通过以下方式访问您的选项:

const matOptionComponent = fixture.debugElement.queryAll(By.directive(MatOption));

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 2019-07-16
    • 2019-08-04
    • 2018-06-01
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 2020-09-17
    相关资源
    最近更新 更多