1

我想通过以角度提交表单来测试结果,但我不确定会发生什么。我已经创建了我的模拟数据并用茉莉花创建了一个间谍,但我写了期望。我知道它返回一个 Observable 但我该如何测试呢?

我的模板

<form #f="ngForm" (ngSubmit)="post_donation_form(f)">
  <!-- form fields -->
</form>

const testForm = <NgForm>{
    value: {
        address: {
            billing_address: '10500 N De Anza Blvd',
            billing_city: ' Cupertino',
            billing_state: 'CA',
            billing_zipcode: '95014'
        },
        billing_email_address: 'test@gmail.com',
        billing_first_name: 'Tim',
        billing_last_name: 'Cook',
        billing_phone_number: 2044567894,
        given_to: 'NumbersUSA Action',
        gross_gift_amount: 1000,
        recurring_subscription: false
    }
};

我的组件

save_donation(form_data: NgForm) {
    return this.donationService.post_donation_form(form_data.value).subscribe(
      data => return data );
}

这是我的测试用例,但我不确定如何编写期望以将其与模拟进行比较。

    it('should post the form values to the server', () => {
            const spy = spyOn(service, 'post_donation_form')
.and.returnValue(Observable.from([testForm]));
            donation_service_component.save_donation(testForm);
            expect(donation_service_component.save_donation).toContain(testForm);
        });
4

0 回答 0