'how to configure a service for unit testing in angular

One of the service(ProductService) is dependent on the LoggerService, want to perform the unit testing ofProductService. Spyobj Loggerservice is registered in the provider of the testbed and in the testcase will get the ProductService using TestBed.Inject(ProductService).

Compared testcase with the one of the example available in the angular.io -> https://angular.io/guide/testing-services In the example both Service and dependent service are registered in the provider of the testbed.

Do we need to register in the provider array of the test bed before getting the ProductService using TestBed.Inject?

            let masterService: MasterService;
            let valueServiceSpy: jasmine.SpyObj<ValueService>;

            beforeEach(() => {
              const spy = jasmine.createSpyObj('ValueService', ['getValue']);

              TestBed.configureTestingModule({
                // Provide both the service-to-test and its (spy) dependency
                providers: [
                  MasterService,
                  { provide: ValueService, useValue: spy }
                ]
              });
              // Inject both the service-to-test and its (spy) dependency
              masterService = TestBed.inject(MasterService);
              valueServiceSpy = TestBed.inject(ValueService) as jasmine.SpyObj<ValueService>;
            });


            export class LogserviceService {

              constructor() { }

              public logMessage(value:string)
              {
                console.log(value);
              }
            }

            export class ProductService {

               products:IProduct[]=[];

              constructor(private logservice:LogserviceService) {
                console.log('getting the service created');
                console.log(logservice);
                console.log(logservice instanceof (LogserviceService));
               }
              getProducts(): IProduct[] {

                this.products =
                [
                  {name:"TV",cost:100,model:"123434",category:"Electronics"},
                 
                ];

                this.logservice.logMessage("I am in the product service");

                return this.products;
              }

              getProduct(category:string):IProduct[]
              {
                this.logservice.logMessage("callling getProduct");

                let products = this.getProducts();
                let prodct= products.filter(x=>x.category ===category);
                return prodct;
              }

            }

            describe('ProductService', () => {
              let service: ProductService;
              let logService: LogserviceService;

              logService = jasmine.createSpyObj(['logMessage']);
              beforeEach(() => {
                TestBed.configureTestingModule({
                  providers: [{ provide: LogserviceService, useValue: logService },
                    
                  ],
                });
              });

             
              it('should get the product list, if valid caterogy is sent', () => {
               
                let service = TestBed.inject(ProductService);  //product service not registered in the providers
                let products = service.getProduct('Electronics');

                expect(products.length).toBeGreaterThan(0);
                expect(logService.logMessage).toHaveBeenCalledWith('callling getProduct');
                expect(products[0].category).toEqual('Electronics');

              });
            });


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source