我需要测试与值的比较。我试过这个例子:
我有这个组件.ts
export class AppComponent implements OnInit {
title= 'Angular'
constructor() { }
ngOnInit() {
}
}
在component.spec.ts
中it(`should have as title 'Angular'`, async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('Angular');
}));
it(`should have as title 'Angular'`, async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('Angular1');
}));
我遵循这个简单的例子。但现在我需要将我的响应值与某个值进行比较
步骤1。 Html组件
<div id="container">
<div id="login_card_container">
<div id="login_card" class="card col s12">
<form [formGroup]="loginForm" (ngSubmit)="onLogin()" class="col s12">
<h1 id="form_title">Login</h1>
<div class="row">
<div class="input-field col s12">
<input formControlName="username" id="username" type="text" class="validate" [ngClass]="{invalid: invalidCredentials}">
<label for="username">Username</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input formControlName="password" id="password" type="password" class="validate" [ngClass]="{invalid: invalidCredentials}">
<label for="password" data-error="Your username/password combination is invalid">Password</label>
</div>
</div>
<div id="login_button_container" class="row">
<button id="login_button" type="submit" class="btn waves-effect waves-light" [disabled]="!loginForm.valid">
<i class="fa fa-sign-in left"></i>
Login
</button>
</div>
</form>
</div>
</div>
</div>
第2步:TS组件
onLogin() {
this.loading = true;
this.ws.login(
this.loginForm.controls['username'].value,
this.loginForm.controls['password'].value)
.subscribe(
result => {
if (result === true) {
} else {
this.loading = false;
this.invalidCredentials = true;
}
});
}
这是我的第一次尝试,并显示此错误
预期间谍日志等于&#39;认证失败&#39;。
实际上这是成功的,因为来自服务的响应是此消息Authentificated failed
it('should notify in console on form submit', () => {
spyOn(console, 'log');
component.loginForm.controls['username'].setValue('admin');
component.loginForm.controls['password'].setValue('11');
fixture.debugElement.query(By.css('form')).triggerEventHandler('ngSubmit', null);
fixture.detectChanges()
expect(console.log).toEqual('Authentificated failed');
});
在此代码中,我希望toEqual
服务响应与('Authentificated failed')
一样,如第一个示例所示
您能否建议我如何将result
表单服务与某些文本进行比较?