Error when testing GET method with Jasmine/Angular

Asked

Viewed 286 times

1

I am trying to compare the type of method when there is a call with the api url.

    import { async, ComponentFixture, TestBed, getTestBed } from '@angular/core/testing';
    import { ServiceOrderTypesComponent } from './service-order-types.component';
    import { CommonModule } from '@angular/common';
    import { MaterialModule } from '../../core/common/material- components.module';
    import { FormsModule } from '@angular/forms';
    import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
    import { ListModule } from '../../core/common/list/list.module';
    import { ServiceOrderTypesRoutingModule } from './service-order-types-routing.module';
    import { ServiceOrderTypeCreateUpdateModule } from './service-order-type-create-update/service-order-type-create-update.module';
    import { HttpClientModule } from '@angular/common/http';
    import { ServiceOrderTypeControllerService } from '../../module/service-order-types';

    import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
    import { Observable } from 'rxjs';
    import 'rxjs/add/observable/of';
    import { environment } from 'environments/environment.dev';

    describe('ServiceOrderTypesComponent', () => {
    let component: ServiceOrderTypesComponent;
    let fixture: ComponentFixture<ServiceOrderTypesComponent>;

    let service: ServiceOrderTypeControllerService;
    let httpMock: HttpTestingController;

    const dummySOT = [{
        'id': 1,
        'name': 'Elétrico03',
        'company': {
            'id': 1,
            'fantasyName': 'Fantasy Name',
            'cnpj': '24073072000127',
            'socialName': 'Name Social',
            'registrationState': '888888',
            'registrationMunicipal': '9999999',
            'street': 'Rua H, 768',
            'complement': 'Complementossss',
            'houseNumber': '232',
            'cep': '69075022',
            'telephoneNumber': '9212324134',
            'contactName': '12321',
            'email': '[email protected]',
            'idBranch': 0
        }
    }];

    class FakeApi {
        getAllUsingGET() {
            return Observable.of(dummySOT);
        }
    }

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            imports: [
                CommonModule,
                FormsModule,
                MaterialModule,
                ServiceOrderTypesRoutingModule,
                ServiceOrderTypeCreateUpdateModule,
                HttpClientModule,
                HttpClientTestingModule,
                // Core
                ListModule,
                BrowserAnimationsModule,
            ],
            declarations: [ServiceOrderTypesComponent],
            providers: [
                { provide: ServiceOrderTypeControllerService, useClass: FakeApi }
            ]

            // providers: [ServiceOrderTypeControllerService]
        }).compileComponents();

        service = TestBed.get(ServiceOrderTypeControllerService);
        httpMock = TestBed.get(HttpTestingController);

    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(ServiceOrderTypesComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });

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


    it('should load the first of getAllUsingGET', () => {
        component.ngOnInit();
        service.getAllUsingGET().subscribe(data => {
            expect(data.length).toBe(1);
            expect(data).toEqual(dummySOT);
        });
    });


    it('should make a GET request', async(() => {
        service.getAllUsingGET();

        const req = httpMock.expectOne(environment.origin.serviceOrderTypes + '/service-order-types/all');
        console.log(req);
        expect(req.request.method).toEqual('GET');
        req.flush([]);
    }));

    it('should get the correct name for service-order-types mocking', () => {
        service.getAllUsingGET().subscribe(data => {
            expect(data[0].name).toBe('Elétrico03');
        });

        const req = httpMock.expectOne(environment.origin.serviceOrderTypes + '/service-order-types/all');
        expect(req.request.method).toBe('GET');
    });

  });

And return these errors:

Serviceordertypescomponent should make a GET request

Error: Expected one matching request for criteria "Match URL: http://localhost:8080/ws-sgm-service-order-types/api/service-order-types/all", found none.
at HttpClientTestingBackend.expectOne (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/common/fesm2015/http/testing.js:332:1)
at UserContext.testing_1.async (http://localhost:9876/_karma_webpack_/webpack:/src/app/containers/service-order-types/service-order-types.component.spec.ts:102:30)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:388:1)
at AsyncTestZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.AsyncTestZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:713:1)
at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:285:1)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:387:1)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runGuarded (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:151:1)
at runInTestZone (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:841:1)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:775:1)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:388:1)

Serviceordertypescomponent should get the correct name and method for service-order-types

Error: Expected one matching request for criteria "Match URL: http://localhost:8080/ws-sgm-service-order-types/api/service-order-types/all", found none.
at HttpClientTestingBackend.expectOne (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/common/fesm2015/http/testing.js:332:1)
at UserContext.it (http://localhost:9876/_karma_webpack_/webpack:/src/app/containers/service-order-types/service-order-types.component.spec.ts:113:30)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:388:1)
at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:288:1)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:387:1)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:138:1)
at runInTestZone (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:509:1)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:524:1)
at attempt (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?73bc53d3671677e6a093fc74e4f9bcde57e5f7ad:4289:46)
at ZoneQueueRunner.QueueRunner.run (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?73bc53d3671677e6a093fc74e4f9bcde57e5f7ad:4217:20)
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.