Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the ideal way to test a service with angular? [closed]

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?

like image 945
Jon Sud Avatar asked Jan 30 '26 06:01

Jon Sud


1 Answers

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);    
  });
  
});
like image 71
Krishna Mohan Avatar answered Feb 01 '26 20:02

Krishna Mohan