Sno*_*man 20 javascript karma-jasmine angular-material angular-cli angular
我有一个角度CLI项目设置.我做了一个使用角度材料组件的表格,比如<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>
Run Code Online (Sandbox Code Playgroud)
这是我的业力规范:
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);
});
});
Run Code Online (Sandbox Code Playgroud)
此时,我只是尝试运行基本单元测试,正确显示标题.
但是,我遇到了很多特定于材料的错误.喜欢
没有MdDialog的提供者.
我点击一个链接打开一个md Dialog.代码在(相当长的).ts文件中,但这不是问题.
我在哪里可以在测试床上添加MdDialog?如果我将它添加到提供者,我会收到错误:"没有覆盖提供者".我不知道如何解决这个问题.
我有什么方法可以配置业力来包括所有材料组件的开始?
谢谢.
ish*_*ood 11
当前的技术要求单独导入Angular Material模块,MaterialModule不推荐使用并在2.0.0-beta.11中删除:
import {
MatButtonModule,
MatIconModule
} from '@angular/material';
Run Code Online (Sandbox Code Playgroud)
然后在TestBed配置中添加与导入相同的列表:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ... ],
imports: [
MatButtonModule,
MatIconModule,
...
],
providers: [ ... ]
})
.compileComponents();
}));
Run Code Online (Sandbox Code Playgroud)
通过调用forRoot()模块来提供所有提供程序
imports: [ MaterialModule.forRoot() ]
Run Code Online (Sandbox Code Playgroud)
对于版本2.0.0-beta.4和更高版本(因为该forRoot方法已删除):
imports: [ MaterialModule ]
Run Code Online (Sandbox Code Playgroud)
对于版本2.0.0-beta.11及更高版本,由于MaterialModule已被删除,因此您必须自己导入测试用例所需的模块:
imports: [ MatButtonModule, MatDialogModule ]
Run Code Online (Sandbox Code Playgroud)
我今天也一直在为此苦苦挣扎,您需要通过在 jasmine 中使用提供程序来自己模拟必要的类。这很麻烦,我希望有更好的方法,但至少没有更多的错误......
如果有人有更好的想法,请启发社区的其他人!
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { AlertDialogComponent } from './alert-dialog.component';
import { MatDialogModule, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
describe('AlertDialogComponent', () => {
let component: AlertDialogComponent;
let fixture: ComponentFixture<AlertDialogComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MatDialogModule],
declarations: [AlertDialogComponent],
providers: [
{
provide: MatDialogRef, useValue: {}
},
{
provide: MAT_DIALOG_DATA, useValue:{}
}
],
}).compileComponents();
}));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16666 次 |
| 最近记录: |