0

I have one service of my own in AngularJS. I have used it in one of my component in Angular 2. Now when i write unit test for the component I am not able to import or inject that service. Can anyone help me out here with this issue.

1 Answer 1

1

You will need to set up the test using the Angular TestBed and inject the service.

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

describe('', () => {
  let fixture: ComponentFixture<TestComponent>;
  let component: TestComponent;
  let service;

  beforeEach(async(() => {

    TestBed.configureTestingModule({
       imports: [
         AppModule
       ],
       declarations: [ 
       ],
       providers: [
       ]}).compileComponents()
      .then(() => {
         fixture = TestBed.createComponent(TestComponent);
         comp = fixture.componentInstance;

         service = TestBed.get(AppService);
      });
   }));
});

Call a service and use spy,

 it('should fetch the cluster list on init', async(() => {
    expect(service.getList).toHaveBeenCalled();
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.