locked
how to post and put request in unit testing with front-end angular and back-end node using jasmine and karma? RRS feed

  • Question

  • I have created one angular project and one node project for add data in SQL workbench.

    I can add data in SQL workbench while I am running ng serve from angular project.

    while the time of unit testing using jasmine & karma and we run ng test command & its run your test case and we got data in console log but we didn't get data in SQL database.

    Form.component.ts
     it(`should set submitted to true`, async(() => {
        component.onSubmit();
        expect(component.submitted).toBeFalsy();
      }));
    
      it('should call the onSubmit method', async(() => {
        spyOn(component, 'onSubmit');
        el = fixture.debugElement.query(By.css('button')).nativeElement;
        el.click();
        expect(component.onSubmit).toHaveBeenCalled();
      }));
    
      it('FormDetails', async(() => {
    
        component.regForm.controls['name'].setValue('Rahil');
        component.regform.controls['email'].setvalue('rahil@gmail.com');
        component.regForm.controls['phone'].setValue('1234567890');
        component.regForm.controls['dob'].setValue('2020-10-30 00:00:00');
        component.regForm.controls['file'].setValue(null);
        component.regForm.controls['password'].setValue('123456zcsascs');
        expect(component.regForm.valid).toBeTruthy();
    
        component.onSubmit();
      }));

    api.services.spec.ts

    it('should add an employee and return it', () => {
        const newData = { name: 'Rahil', email: 'rahil@gmail.com', phone: '1234567890', dob: '2020-10-30', file: null, password: '123456zcsascs' };
        const url: string = "http://localhost:5000/api/v1/employees/add";
        apiService.addData(newData).subscribe((dataAdded) => {
          expect(dataAdded).toEqual(newData);
        }
        );
        const req = httpTestingController.expectOne("http://localhost:5000/api/v1/employees/add");
        console.log("API called");
        expect(req.request.method).toEqual('POST');
        expect(url).toBe(url);
        expect(req.cancelled).toBeFalsy();
        expect(req.request.responseType).toEqual('json');
        req.flush(newData);
        console.log("End");
      });


    Can anyone help me for the same?

    Thanks in Advance

    • Edited by BhoomeshJoshi Tuesday, November 3, 2020 7:33 AM update question
    Tuesday, November 3, 2020 7:04 AM

Answers

All replies