javascript - 如何在 Angular 单元测试中使 Material 组件与 Karma 一起工作

标签 javascript angular angular-material karma-jasmine angular-cli

我设置了一个 Angular CLI 项目。我制作了一个使用 Angular Material 组件的表格,例如 <md-card> .

我刚开始编写我的第一个 Karma/Jasmine 单元测试,遵循 angular docs 中的步骤.

这是我的组件模板:

<md-card [ngClass]="'dialog-card'">
<md-card-title [ngClass]="'dialog-title'">
    {{title}}
</md-card-title>
<md-card-content>

    <form (ngSubmit)="login()" #loginForm="ngForm">

        <md-input-container class="md-block">
            <input md-input [(ngModel)]="user.email" 
                name="userEmail" type="email" placeholder="Email" 
                ngControl="userEmail" 
            required>
        </md-input-container>
        <br>

        <md-input-container class="md-block">
            <input md-input [(ngModel)]="user.password" 
                name="userPassword" type="password" placeholder="Password" 
                ngControl="userPassword" 
            required>
        </md-input-container>
        <br>

        <tm-message msgText="Wrong username or password" *ngIf="showError"></tm-message>
        <br>


        <button md-button type="submit" [disabled]="!loginForm.form.valid">Login</button>

        <p (click)="openForgotPasswordModal()">Forgot Password?</p>

    </form>

</md-card-content>

这是我的 karma 规范:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By }              from '@angular/platform-browser';
import { DebugElement }    from '@angular/core';
import { MaterialModule, MdDialogRef, MdDialog  } from '@angular/material';
import { FormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';

import { TmLoginComponent } from './tm-login.component';
import { TmMessageComponent } from '../../shared/components/tm-message.component';
import { UserAuthenticationService } from '../login/user-authentication.service';

describe('TmLoginComponent (inline template)', () => {

let comp: TmLoginComponent;
let fixture: ComponentFixture < TmLoginComponent > ;
let de: DebugElement;
let el: HTMLElement;

beforeEach(() => {
    TestBed.configureTestingModule({
        declarations: [TmLoginComponent, TmMessageComponent], // declare the test component
        imports: [MaterialModule, FormsModule,
            RouterTestingModule.withRoutes(
                [{
                    path: 'login',
                    component: TmLoginComponent
                }, ])
        ],
        providers: [UserAuthenticationService],

    });

    fixture = TestBed.createComponent(TmLoginComponent);

    comp = fixture.componentInstance; // TmLoginComponent test instance

    // query for the title <h1> by CSS element selector
    de = fixture.debugElement.query(By.css('.title'));
    el = de.nativeElement;
});

    it('should display original title', () => {
        fixture.detectChanges();
        expect(el.textContent).toContain(comp.title);
    });
});

此时,我只是尝试运行基本单元测试以确保标题正确显示。

但是,我遇到了很多 Material 特定的错误。喜欢

No provider for MdDialog.

我在单击链接时打开了一个 md 对话框。代码在(相当长的).ts 文件中,但这不是这里的问题。

我应该在测试台的什么地方添加 MdDialog?如果我将它添加到提供商,我会收到错误消息:“没有覆盖提供商”。我不知道如何解决这个问题。

有什么方法可以配置 karma 以在开始时包含所有 Material 组件?

谢谢。

最佳答案

当前技术要求单独导入 Angular Material 模块,因为 MaterialModule 已弃用并且 was removed in 2.0.0-beta.11 :

import {
    MatButtonModule,
    MatIconModule
} from '@angular/material';

然后在 TestBed 配置中添加与导入相同的列表:

beforeEach(async(() => {
    TestBed.configureTestingModule({
        declarations: [ ... ],
        imports: [
            MatButtonModule,
            MatIconModule,
            ...
        ],
        providers: [ ... ]
    })
        .compileComponents();
}));

关于javascript - 如何在 Angular 单元测试中使 Material 组件与 Karma 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41484008/

相关文章:

javascript - 图像的鼠标按下、鼠标移动和鼠标弹起事件?

html - border-radius-top-left 不适用于 Angular2

css - 将大图像拟合到特定尺寸

Angular MatPaginator 和 Azure 表存储

angular - 单击 Angular 7 中的另一个组件时从 mat-table 传递数据行

Angular4 Material 输入显示不正确

javascript - md-autocomplete ng-pattern ="..."不起作用

javascript - MySQL NodeJS 错误 : Cannot enqueue Query after invoking quit using transactions

javascript - 删除行 Jtable Jquery

javascript - 使用 Socket.io 发送 TCP 消息时出现问题