【问题标题】:Error: Expected spy MovieService.getWatchListedMovies to have been called. angular Unit Testing错误:应调用间谍 MovieService.getWatchListedMovies。角度单元测试
【发布时间】:2019-03-01 13:13:55
【问题描述】:
  • 一个名为 watchlist 的组件文件,它依赖于 MovieService(service) 来获取电影。

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

  • 组件代码如下,

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()
       );

  }
  • 使用 movieService(Service) 上的 jasmine.spy 对象对 component(watchlist) 进行单元测试

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

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
  });
});

`MovieModule 我在其中注册了 watchlistMovieService.,如下所示

@NgModule({
  imports: [
    CommonModule,
    HttpClientModule,
    MovieRouterModule,
    MatCardModule,
    MatButtonModule,
    MatSnackBarModule,
    FormsModule,
    ReactiveFormsModule,
    MatInputModule
      ],
  declarations: [ContainerComponent,TmdbContainerComponent,TumbnailComponent, WatchlistComponent, MovieDetailsComponent],
  
  exports:[
    MovieRouterModule,
    TumbnailComponent,
    ContainerComponent,
    TmdbContainerComponent
  ],
  providers:[
    MovieService
  ]
})
export class MovieModule { }

【问题讨论】:

  • 大家好,请查看。
  • 你好阿伦,你能在一些句子上加点字符,这样你的问题就更易读了。避免使用长句。还要解释上下文、语言并提供更多信息。提供一些作为 Angular 或单元测试的键是好的,但还不够。我已经把你的问题读了 3 遍,我认为它可以改进。
  • 尝试将getWatchListedMovies.and.callFake(() =&gt; { return of(stubTmdbMovies) }); 替换为getWatchListedMovies.and.callFake(() =&gt; console.log("I have been called")); 并检查是否将消息打印到控制台
  • @schlebe 感谢您的反馈。我会更新我的问题。
  • @AmirArbabian 我试过了,只有空的控制台日志。还有其他解决办法吗?

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


【解决方案1】:

只是猜测,尝试移动线

movieServiceFake = TestBed.get(MovieService);

进入测试开始,喜欢

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

【讨论】:

  • @Amit 里面的移动“tesdbed.get”也没有解决问题。
  • 好的,你能添加代码你如何在模块中注册WatchlistComponentMovieService吗?这会有所帮助。
  • @Amit 我已经发布了 module.ts 文件。请看看并帮助我。
  • 好的,你能检查它是否是MovieService的唯一注册,因为你可以在@Component装饰器中的组件级别上注册一个MovieService,因此你可以获得真正的服务你的组件反而嘲笑了一个
猜你喜欢
  • 1970-01-01
  • 2019-02-24
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多