how to check function is called or not in angular?

前端 未结 1 1051
野的像风
野的像风 2020-12-19 16:03

I am trying to test component in angular .following thing I need to test

  1. service function is called or not
  2. how to mock the response
相关标签:
1条回答
  • 2020-12-19 16:48

    you can mock the service that way:

    const mockPosts: Posts = {
       userId: 10,
       id: 10,
       title: "post",
       body: "post"};
    
    class MockAppService extends AppService{
    
       public getData() {
          return Observable.of(mockPosts)
       }
      }
    

    and use that mock class in your providers instead of the service

     { provide: AppService, useClass: MockAppService },
    

    and add this:

     fixture = TestBed.createComponent(AppComponent);
     component = fixture.componentInstance;
     appservice = TestBed.get(AppService); // this line
    

    you can spyOn that service and return a value like this

    spyOn(appservice , 'getData').and.returnValue("your value")
    

    final file

       import { ComponentFixture,TestBed, async,getTestBed } from '@angular/core/testing';
      import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
      import { HttpClientModule, HttpClient } from '@angular/common/http';
    
      import { AppComponent } from './app.component';
      import { AppService } from './app.service';
      import { Observable } from 'rxjs/Observable';
      import { Posts } from './post.interface';
      import 'rxjs/add/observable/of';
    
      const mockPosts: Posts = 
      {userId: 10,
      id: 10,
      title: "post",
      body: "post",};
     class MockAppService extends AppService {
     public getData(){
        return Observable.of(mockPosts)
       }
     }
    
     describe('AppComponent', () => {
      let fixture:ComponentFixture<AppComponent>,
      component:AppComponent,
      injector:TestBed,
      service:AppService,
      httpMock:HttpTestingController,
      el:HTMLElement;
     beforeEach(async(() => {
     TestBed.configureTestingModule({
       imports: [HttpClientTestingModule],
      declarations: [
        AppComponent
       ],
       providers: [ 
        { provide: AppService, useClass: MockAppService }
       ]
    
       }).compileComponents();
        fixture = TestBed.createComponent(AppComponent);
        component = fixture.componentInstance;
        service = TestBed.get(AppService)
        // injector = getTestBed();
        // service = injector.get(AppService);
        // httpMock = injector.get(HttpTestingController);
        spyOn(service,'getData');
      }));
    
      afterEach(() => {
      //httpMock.verify();
      });
      fixture = TestBed.createComponent(AppComponent);
      component = fixture.componentInstance;
      service = TestBed.get(AppService)
      // injector = getTestBed();
      // service = injector.get(AppService);
      // httpMock = injector.get(HttpTestingController);
    
      spyOn(service,'getData');
    
    
      describe('AppComponent onit test', () => {
      it('should create the app', async(() => {
        expect(true).toBe(true);
      }));
      it('should called appService getData method', async(() => {
          fixture.detectChanges(); 
         expect(service.getData).toHaveBeenCalled();
      }));
      })
     });
    
    0 讨论(0)
提交回复
热议问题