【问题标题】:Angular Unit testing : this.router.myMethod is not a functionAngular 单元测试:this.router.myMethod 不是函数
【发布时间】:2017-11-07 13:02:46
【问题描述】:

在 Angular 4.0.0 下进行单元测试

我的测试用例如下:

test.spec.ts:

        // Test Suite of Que-Again features
            describe('QueueAgainComponent features', () => {

                it('should navigate', () => {
                    comp.goToPrevious(); // HERE IS THE PROBLEM
                    expect(mockRouter.navigate).toHaveBeenCalledWith(['/home/pilote'], {skipLocationChange: true});
                    sharedclientService.user.isAdviser = true;
                    comp.goToPrevious();
                    expect(mockRouter.navigate).toHaveBeenCalledWith(['/home/advisor'], {skipLocationChange: true}); 
       });

在配置部分我模拟了我的 RouteNavigator 服务,如下所示:

        let mockRouter = {
            navigate: jasmine.createSpy('navigate')
        };
    providers: [{provide: RouteNavigator, useValue: mockRouter}]

我的测试失败并抛出 tghis 错误:

TypeError: this.router.myMethod 不是函数

日志文件说问题点在 98:18 正是这一行:

comp.goToPrevious();

因此 goToPrevious() 在我的组件中实现如下:

      goToPrevious() {
        this.routeNavigator.myMethod();
      }

routeNavigator指的是一个自定义服务,用来处理自定义重定向,像这样:

**Route-navigator.service.ts**
    import { LoginComponent } from '../../login/login.component';
    import { SharedclientService } from '../../service/sharedclient.service';
    import { Injectable } from '@angular/core';
    import { Router, ActivatedRoute } from '@angular/router';

    @Injectable()
    export class RouteNavigator {
      constructor(private router: Router, private sharedclientService: SharedclientService, private activatedRoute: ActivatedRoute) { }

      accessAdvancedSearchFromRegistration = false;
      accessSyntheseWhileGdfaShown = false;
      track360View = false;
      oldUrl: string;

      private rdvReferer: string;
      public navigateTo(url: string) {
        this.oldUrl = this.router.url;
        this.router.navigate([url], { skipLocationChange: true });
      }

      public myMethod()  // HERE IS MY METHOD
     {

        if(this.sharedclientService.user != null && this.sharedclientService.user.isAdviser==null){
            this.navigateTo('/home/blank');
            this.sharedclientService.setCurrentRole('');
        }

        else  if (this.sharedclientService.user != null && this.sharedclientService.user.isAdviser) {
          this.navigateTo('/home/advisor');
          this.sharedclientService.setCurrentRole('VENDEUR');
        } else {
          this.navigateTo('/home/pilote');
          this.sharedclientService.setCurrentRole('PILOTE');
        }
      }

      public goToNextAdvancedSearch() {
        if (this.accessAdvancedSearchFromRegistration || !this.sharedclientService.user.isAdviser) {
          this.navigateTo('/home/registration');
        }
        else {
          this.track360View = true;
          this.navigateTo('/home/view-360');
        }
      }

      public exitAdvancedSearch() {
        if (this.accessAdvancedSearchFromRegistration) {
          this.navigateTo('/home/registration');
        }
        else {
          this.goToHomeAccordingToProfile();
        }
      }

      goToRdv(url?:string) {
          if(!url)
        this.rdvReferer = this.router.url;
          else this.rdvReferer=url;
        this.navigateTo('/home/appointment');
      }

      exitRdv() {
        this.navigateTo(this.rdvReferer);
      }
      goToBlank() {
          this.navigateTo('/home/blank');
        }
      public url() {
        return this.router.url;
      }
     }

关于问题出在哪里或如何处理它的任何想法?

【问题讨论】:

    标签: javascript angular unit-testing typescript jasmine


    【解决方案1】:

    因为在goToPrevious() 你使用this.router.myMethod();

    除非您扩展了路由器,否则我在 Angular 的路由器模块中不知道这一点!

    EDIT 你的模拟应该包含这个:

    let mockRouter = {
      navigate: jasmine.createSpy('navigate'),
      myMethod: () => {/* mock it as you want here */}
    };
    providers: [{provide: RouteNavigator, useValue: mockRouter}]
    

    【讨论】:

    • 是的,它不是默认路由器,而是扩展路由器,我附上了它的实现
    • 好的,那你能告诉我你在测试中是如何模拟它的吗?
    • 我已经更新了我的所有问题,以便为您提供更多详细信息;)
    • 我现在看到了。在您的模拟中,您声明了导航方法,但您没有声明 myMethod !由于未声明,因此测试平台不知道他要做什么。尝试实现它并查看下一个错误!
    • 我没明白;更多细节?
    猜你喜欢
    • 2018-11-29
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    相关资源
    最近更新 更多