0

I have angular 4 application and i am writing test cases using jasmin + Karma

My first test case working properly as it creates component and service instance,

But second test cases is failing as it is not able to fetch mock data for 'getorders' method, when i log its value is undefined.

    beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [ OrderListComponent ],
          providers: [
            OrderFormFetchService,
          ],
          imports: [
            BrowserModule,
            BrowserAnimationsModule,
            FormsModule,
            ReactiveFormsModule,
            HttpClientModule,
            RouterModule.forRoot([]),
            NglModule.forRoot({svgPath: 'assets/icons'}),
          LayoutModule,
          NgbModule.forRoot()
        ],
        })
        .compileComponents();
      }));


 beforeEach(() => {
        orderFormFetchService = TestBed.get(OrderFormFetchService);
        fixture = TestBed.createComponent(OrderListComponent);
        component = fixture.componentInstance;
        orderListStub = [{
          'id': '5c18e74dd5ab9332cc473c94',
          'createdAt': '2018-12-18T12:25:49.507Z',
          'updatedAt': '2018-12-18T12:25:49.507Z',
          'name': 'apple',
          'website': 'www.apple.com',
          'secsId': ''
        }];
        spyOn(orderFormFetchService, 'getorders').and.returnValue(Observable.of(orderListStub));
        fixture.detectChanges();
      });

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

     it('check order list ', () => {
      console.log(orderFormFetchService.getorders()); // -> undefined
      expect(component.orderList).toBeDefined();
      //  expect(component.orderList).toEqual(orderListStub);
      });

Code in OrderListComponent is as follows,

export class OrderListComponent   extends BaseComponent implements OnInit {

  defaultQuery = {website: null, name: null};
  orderList: any;
  query = _.clone(this.defaultQuery);
  date: Date;

  constructor(private orderFormFetchService: OrderFormFetchService) { super(); }

  ngOnInit() {
    this.date = new Date();
    this.getOrderList();
  }

  getOrderList(): void {
    this.orderFormFetchService.getOrders()
      .subscribe((res: Response) => {
        if (_.isArray(res)) {
          this.orderList = _.orderBy(res, [order => order.name.toLowerCase()], ['asc']);
        } else {
          this.orderList = [res];
        }
      });
  }
  }

Please help

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Raj N
  • 249
  • 1
  • 18
  • Can you show the code of the `OrderListComponent`? And I think this `orderFormFetchService = TestBed.get(orderFormFetchService); fixture = TestBed.createComponent(orderListComponent);` should be `orderFormFetchService = TestBed.get(OrderFormFetchService); fixture = TestBed.createComponent(OrderListComponent);` (capital O's). – Fabian Küng Jan 07 '19 at 10:06
  • @FabianKüng, I have updated with component code – Raj N Jan 07 '19 at 13:14
  • `spyOn(orderFormFetchService, 'getorders').and.returnValue(Observable.of(orderListStub));` make sure you name the correct function, should be `getOrders` and also I think the spy should return the same type as the original function, which would be of type `Response`I assume? Additionally I would define the spy in the test itself like this: `const mySpy = spyOn(..)` and then you can check if your spy actually gets called like this `expect(mySpy).toHaveBeenCalled();`. – Fabian Küng Jan 07 '19 at 13:26
  • From service its returning Observable but in component this method subscribe with Response return type – Raj N Jan 07 '19 at 15:56
  • Maybe we are missing some crucial information but when I try to reproduce a it in a stackblitz it works fine: https://stackblitz.com/edit/testing-jnllr6?file=app/order-list/order-list.component.spec.ts – Fabian Küng Jan 08 '19 at 09:55
  • this link is not working for me – Raj N Jan 10 '19 at 19:11
  • @FabianKüng.. could you please give me unit testing example for component using order service to initialize orderlist in component , test with single order and multiple orders array – Raj N Jan 10 '19 at 19:27

0 Answers0