对Http.post和应用程序控制台内容的空白ID进行运行测试

时间:2019-04-30 15:15:33

标签: angular typescript unit-testing jasmine

我正在测试角度应用程序,当我不向该API传递任何数据时,它显示“没有要返回的数据”,当我传递accountID时,它返回整个帐户详细信息的JSON格式。

我想测试在传递空白accountId时,应用程序控制台中显示的文本是否确实是“没有要返回的数据”。

component.ts

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="cars">
      <option value=""></option>
      <option value="ferrari">Ferrari</option>
      <option value="lamborghini">Lamborghini</option>
      <option value="porsche">Porsche</option>
      <option value="porsche">Mercedes</option>
      <option value="porsche">Mazda</option>
    </select> 
      <input class="SubmitButton" type="submit" value="Go to Car">

component.html

loadData() {
    this.http.post('/api/accountid', this.record)
    .subscribe(res => {
        const status = res['status'];
        if (res['status'] == "FAILURE") {
          this.consoleMessages="No Data to be returned"; 
         else {
          this.consoleMessages = "Sucess" + JSON.stringify(res);

          });

        }
      }, (err) => {
        console.log(err);
        this.consoleMessages = "No Data to be returned";
      }
    );
  }

1 个答案:

答案 0 :(得分:0)

注意:我的问题的更多信息。这是我创建的规范文件,但无法正常工作

it(`should display value No Data to be returned when I don't pass data to API `,()=>{
    let spyOnLoadCustomer=spyOn(component,"loadData").and.callThrough();
    let formData=fixture.debugElement.queryAll(By.css('.form-group'));
    let loadButton=formData[3].query(By.css('button')).triggerEventHandler('click','');//I pass null
    expect(component.consoleMessages).toEqual('No Data to be returned');//fails

//I will pass accountId for this API

 loadButton=formData[3].query(By.css('button')).triggerEventHandler('click','_123456');//I pass null
 expect(component.consoleMessages).toEqual('Sucess.......some json');
})