首页 > 解决方案 > 日志上的预期间谍等于“认证失败”

问题描述

我需要测试与价值的比较。我试过这个例子:

我有这个component.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;
        }
      });
  }

这是我的第一次尝试,并显示此错误

日志上的预期间谍等于“认证失败”。

事实上这是成功的,因为来自服务的响应就是这个消息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表单服务与一些文本进行比较吗?

标签: angulartypescripttestingjasminekarma-jasmine

解决方案


推荐阅读