Angular Jasmine tests that continue upon previous tests

Discussão em 'Angular' iniciado por Kadiem Alqazzaz, Dezembro 21, 2024.

  1. I have a service called items here is the spec file:

    import { TestBed } from '@angular/core/testing';

    import { ItemsService } from './items.service';

    describe('ItemsListService', () => {
    let service: ItemsService;

    beforeEach(() => {
    TestBed.configureTestingModule({});
    service = TestBed.inject(ItemsService);
    });

    it('should be created', () => {
    expect(service).toBeTruthy();
    });

    it('should insert new item', () => {
    const item = {
    id: '123',
    name: 'item',
    baseUnit: 'unit',
    brand: 'brand',
    price: 100,
    quantity: 10,
    supplier: 'supplier',
    cost: 50,
    imageUrl: 'url',
    };
    service.insertItem(item);
    const items = service.getItems();
    expect(items.length).toBeGreaterThan(0);
    expect(items).toContain(item);
    });
    });


    And here is another service called orders that depends on items service to create orders from items stored.

    import { TestBed } from '@angular/core/testing';

    import { OrdersService } from './orders.service';

    describe('OrdersService', () => {
    let service: OrdersService;

    beforeEach(() => {
    TestBed.configureTestingModule({});
    service = TestBed.inject(OrdersService);
    });

    it('should be created', () => {
    expect(service).toBeTruthy();
    });

    it('should insert new order', async () => {
    const newOrder = await service.insertOrder(itemsService.getItems()[0]);
    });
    });


    So in order for orders service to create a new order it needs to access the item stored inside items service here service.insertOrder(itemsService.getItems()[0]).

    How do I access the same instance of items service created in the spec file?

    Continue reading...

Compartilhe esta Página