错误:预期已调用间谍MovieService.getWatchListedMovies。角度单位测试

时间:2019-02-23 15:20:28

标签: angular unit-testing jasmine karma-jasmine spy

  • 名为监视列表的组件文件,该文件取决于 MovieService(服务)以获取电影。

  • 调用ngOnInit()将调用MovieService.getWatchlistedMovies()

  • 组件代码如下:

Highcharts.setOptions({
    time: {
       useUTC: false
    }
});

$(function() {
  $('#container').highcharts({
 series: [{
  "name": "avg_sales",
  "color": "#3b6982",
  "data": [{
      "x": 1550862788000,
      "y": 526.4200000000001
    }, {
      "x": 1550866388000,
      "y": 1850.3116666666667
    }, {
      "x": 1550869988000,
      "y": 3199.786
    }]
}],
tooltip: {
  dateTimeLabelFormats: {
    hour: '%A, %b %e, %l %p'
  },
},
xAxis: {
  type: 'datetime',
  dateTimeLabelFormats: {
        millisecond: '%e. %b %I:%M %P',
        second: '%e. %b %I:%M %P',
        minute: '%e. %b %I:%M %P',
        hour: '%e. %b %I:%M %P',
        day: '%e. %b %I:%M %P',
        week: '%e. %b %I:%M %P',
        month: '%e. %b %I:%M %P',
        year: '%e. %b %I:%M %P'
    },
  },

 });
});

    使用 movieService(Service)

    上的 jasmine.spy 对象在组件(监视列表)上进行
  • 单元测试/ li>

  • watchlist.spec.ts文件代码如下,

export class WatchlistComponent implements OnInit {

  movies: Array<Movie>;
  movieType:string;
  
  constructor(private movieService:MovieService,private route:ActivatedRoute) { 

    this.movies=[];
    this.route.data.subscribe((data)=>{
      this.movieType=data.movieType
    });
   
  }

  ngOnInit() {

    this.movieService.getWatchListedMovies()
    .subscribe((movies)=>{
     this.movies.push(...movies);
       },
       this.handleErrors()
       );

  }

` MovieModule ,我在其中注册了监视列表 MovieService。,如下所示

describe('WatchlistComponent', () => {
  let component: WatchlistComponent;
  let fixture: ComponentFixture<WatchlistComponent>;
  let movieServiceFake:jasmine.SpyObj<MovieService>;
  let movieService;
  let stubTmdbMovies: Movie[];
  beforeEach(async(() => {
    movieServiceFake = jasmine.createSpyObj('MovieService', ['getWatchListedMovies']);


    TestBed.configureTestingModule({
      imports: [MovieModule,
        RouterTestingModule,
        HttpClientTestingModule],
      providers: [{ provide: MovieService, useValue: movieServiceFake }]
    })
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(WatchlistComponent);
    component = fixture.componentInstance;
    movieServiceFake = TestBed.get(MovieService);
  });

  it('should create watchlist', () => {
    expect(component).toBeTruthy();
  });

  it('should call ngOnInIt', () => {
    //Arrange
    let spyOnComponent = spyOn(component, 'ngOnInit');
    movieServiceFake.getWatchListedMovies.and.callFake(() => { return of(stubTmdbMovies) });
    //Act
    component.ngOnInit();
    //Assert
    expect(spyOnComponent).toHaveBeenCalled();
    expect(movieServiceFake.getWatchListedMovies).toHaveBeenCalled();//error line
  });
});

1 个答案:

答案 0 :(得分:0)

只是一个猜测,请尝试移动行

movieServiceFake = TestBed.get(MovieService);

进入测试开始,例如

it('should call ngOnInIt', () => {
    movieServiceFake = TestBed.get(MovieService);
    ....
});