3

I'm new to angular testing and I want to create tests for my service.

The problem is my service (foo) is inject another service (bar) in its constructor.

So I should create a mock to bar? mock every function inside bar? if so, I need to do that for every package I import? write my own functions as mock?

Jon Sud
  • 10,211
  • 17
  • 76
  • 174

1 Answers1

1

You can create either a mock/stub or make use of jasmine spies.

 export const jobServiceStub: Partial<JobService> = {
   getUser() {
     return {};
   }
 }

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

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [        
        //replacing service with stub
        { provide: JobService, useValue: jobServiceStub },        
        UtilityService
      ]
    });
    service = TestBed.inject(SampleService);
  });
  

  it('should get all data', () => {
    const utilityService = TestBed.inject(UtilityService);    
    const spy = spyOn(utilityService, 'getValues').and.returnValue(2);    
  });
  
});
Krishna Mohan
  • 1,612
  • 3
  • 19
  • 27