我正在使用Angular编写我的第一个组件测试之一,并且我在使ngModel绑定工作方面遇到了一些困难。 这是我的测试模块定义:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
LdapLoginComponent,
],
imports: [
CommonModule,
FormsModule,
NoopAnimationsModule,
MatInputModule,
MatFormFieldModule,
RouterTestingModule,
],
providers: [
{
provide: AuthorizationService,
useValue: { login() {} },
},
]
}).compileComponents();
}));
这是我的测试用例:
it('should bind form fields with class', fakeAsync(() => {
// Given
const username = 'username';
const password = 'password';
const usernameField = de.query(By.css('input[name=username]')).nativeElement;
const passwordField = de.query(By.css('input[name=password]')).nativeElement;
// When
usernameField.value = username;
passwordField.value = password;
usernameField.dispatchEvent(new Event('input'));
passwordField.dispatchEvent(new Event('input'));
tick();
fixture.detectChanges();
// Then
expect(comp.username).toEqual(username);
expect(comp.password).toEqual(password);
}));
我的组件类:
export class LdapLoginComponent {
username: string;
password: string;
errorMessage: string;
submitDisabled = false;
constructor(
private authorizationService: AuthorizationService,
private router: Router,
) {
}
login(): void {
delete this.errorMessage;
this.submitDisabled = true;
this.authorizationService.login(AuthorizationProvider.LDAP, this.username, this.password)
.subscribe(
() => {
this.router.navigate(['/']);
},
(err: Error) => {
this.errorMessage = err.message;
this.submitDisabled = false;
},
);
}
}
我的组件模板:
<form class="form-container" (submit)="login()">
<mat-form-field color="warn">
<input
matInput
type="text"
name="username"
placeholder="Insert your username"
[(ngModel)]="username"
required
i18n-placeholder="@@input.placeholder.username">
</mat-form-field>
<mat-form-field color="warn">
<input
matInput
type="password"
name="password"
placeholder="Insert your password"
[(ngModel)]="password"
required
i18n-placeholder="@@input.placeholder.password">
</mat-form-field>
<button
mat-raised-button
type="submit"
color="warn"
[disabled]="submitDisabled"
i18n="@@input.submit">Submit</button>
</form>
<article>{{errorMessage}}</article>
我正在更改测试中的用户名和密码字段的值,并且我希望相应地更新我的类的用户名和密码字段。如果我在浏览器中手动测试,但测试中没有测试,那么一切都很顺利。
有什么想法吗?
感谢。
答案 0 :(得分:1)
对于mat-input字段,我们需要在更改输入之前执行focus():
it('should bind form fields with class', fakeAsync(() => {
// Given
const username = 'username';
const password = 'password';
const usernameField = de.query(By.css('input[name=username]')).nativeElement;
const passwordField = de.query(By.css('input[name=password]')).nativeElement;
usernameField.focus();
passwordField.focus();
// When
usernameField.value = username;
passwordField.value = password;
usernameField.dispatchEvent(new Event('input'));
passwordField.dispatchEvent(new Event('input'));
tick();
fixture.detectChanges();
// Then
expect(comp.username).toEqual(username);
expect(comp.password).toEqual(password);
}));
答案 1 :(得分:0)
问题是在调用dispatchEvent之前,实际上并没有设置输入字段的值。您正在直接设置组件属性。
comp.username = username;
usernameField.dispatchEvent(new Event('input'));
应该是
let usernameFieldElement = usernameField.nativeElement;
usernameFieldElement.value = username;
usernameField.dispatchEvent(new Event('input'));
和密码相同。
另一件事是你要同时测试三件事,将文本输入到输入区域,单击按钮会运行登录和登录功能本身。我建议将这些分为三个断言。
如上所述设置字段并检查数据绑定。
点击按钮并监视登录功能以检查它是否已被调用。
设置实际组件属性并直接调用logon()并检查您的navigateSpy是否已正确调用。
这样一来,如果出现问题,您将能够更轻松地找到它。