繁体   English   中英

角单元测试:this.router.myMethod不是函数

[英]Angular Unit testing : this.router.myMethod is not a function

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

关于问题是什么或如何解决的任何想法?

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

除非您扩展了路由器,否则我在Angular的路由器模块中对此一无所知!

编辑您的模拟应包含以下内容:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM