微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我如何使用业力在角度应用程序中测试if语句分支

如何解决我如何使用业力在角度应用程序中测试if语句分支

我是测试的新手,想知道你们中的一个人是否可以帮助我了解如何测试分支。特定的代码段是我正在尝试与其他if语句一起测试的SubmitContent方法中的if语句-让我知道您是否需要更多信息,并感谢您抽出宝贵时间来看看

这是组件中的代码

ngOnInit(): void {
    this.preventBackButton();
    const modal = this.bsModalService.show(LoadingComponent,{
      animated: false,ignoreBackdropClick: true,});
 
    this.form = this.formBuilder.group({
      contactAllowed: [undefined],});
 
    this.apiService
      .getPaymentSuccessDetails()
      .pipe(finalize(() => modal.hide()))
      .subscribe((e) => {
        this.apiResponse = e;
        this.paidByDirectDebit = !!e.data.renewalPerformProceedToPurchaseResult
          .confirmationDetails.directDebit;
        this.paidUpFrontByCard = !e.data.renewalPerformProceedToPurchaseResult
          .confirmationDetails.directDebit;
        this.apiService.sendData(this.apiResponse.data.googleECommerceTrackingDetails);
        this.getConsentDetails();
        console.log("e",e);
      });
 
    this.subscriptions.push(
      this.t4CmsService
        .getConfirmationPageRightHandPanel()
        .subscribe((html) => {
          this.rhsContent = html;
        })
    );
 
    this.consentUpdateStatus = false;
    this.consentUpdateClicked = false;
  }
 
  getConsentDetails() {
    this.apiService.getConsentDetails().subscribe((e) => {
      this.apiConsentResponse = e;
      const data = this.apiConsentResponse.data;
      const key = "contactAllowed";
 
      if (data) {
        if (!data?.contactAllowed) {
          this.form.controls[key].setValue(data.contactAllowed);
        }
      }
      console.log("e",e);
    });
  }
 
  submitConsent(e) {
    this.consentUpdateClicked = false;
    if (e.target) {
      const modal = this.bsModalService.show(LoadingComponent,{
        animated: false,});
 
      this.apiService
        .setConsentDetails({ contactAllowed: this.form.value.contactAllowed })
        .pipe(finalize(() => modal.hide()))
        .subscribe((e) => {
          this.apiConsentResponse = e;
          ***if (this.apiConsentResponse?.status === -1) {
            this.consentUpdateStatus = false;
          } else {
            this.consentUpdateStatus = true;
          }***
          this.consentUpdateClicked = true;
        });
    }
  }

这是规范中的代码(除最后一个测试外,所有测试都通过了,这是我正在研究的内容):

import { async,ComponentFixture,Testbed } from "@angular/core/testing";

import { PaymentSuccessComponent } from "./payment-success.component";
import { ApiService } from "src/app/shared-services/api/api.service";
import { MockApiService } from "src/app/shared-services/api/api.service.mock";
import { HttpClient } from "@angular/common/http";
import { BsModalService } from "ngx-bootstrap";
import { FormBuilder } from "@angular/forms";
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

describe("PaymentSuccessComponent",() => {
  let component: PaymentSuccessComponent;
  let fixture: ComponentFixture<PaymentSuccessComponent>;

  beforeEach(async(() => {
    Testbed.configureTestingModule({
      schemas: [CUSTOM_ELEMENTS_SCHEMA],declarations: [PaymentSuccessComponent],providers: [
        {
          provide: ApiService,useClass: MockApiService,},{
          provide: HttpClient,useValue: {
            dummy: jasmine.createSpy("dummy"),FormBuilder,{
          provide: BsModalService,useValue: {
            show: () => {},],}).compileComponents();
  }));

  beforeEach(() => {
    fixture = Testbed.createComponent(PaymentSuccessComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it("should create",() => {
    expect(component).toBeTruthy();
  });

  it("should getConsentDetails and not invalidate the component",() => {
    component.getConsentDetails();
    expect(component).toBeTruthy();
  });

  it("should call submitConsent and not invalidate the component",() => {
    const ev = new Event("click");
    component.submitConsent(ev);
    fixture.detectChanges();
    expect(component).toBeTruthy();
  });


it("should check if branch is true",() => {
    const spy =  spyOn(component,"submitConsent");
    const ev = new Event("click");
    component.apiConsentResponse.status  = -1; // if statement argument
    component.submitConsent(ev);       // should execute if part
    expect(spy).toHaveBeenCalled();
  });
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。