gpt4 book ai didi

Angular Testing 和 ngModel

转载 作者:行者123 更新时间:2023-11-28 19:59:50 25 4
gpt4 key购买 nike

我正在用 Angular 编写我的第一个组件测试,我在使 ngModel 绑定(bind)工作时遇到了一些困难。这是我的测试模块定义:

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>

我正在更改我的测试中用户名和密码字段的值,我希望我的类的用户名和密码字段相应地更新。如果我在浏览器中手动测试一切正常,但在测试中却不行。

有什么想法吗?

谢谢。

最佳答案

看起来像垫输入字段,我们需要在更改输入之前执行 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);
}));

关于 Angular Testing 和 ngModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48205291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com