简体   繁体   English

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

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

Am worrking on unit testing , under Angular 4.0.0 在Angular 4.0.0下对单元测试感到担忧

My test case is the following : 我的测试用例如下:

test.spec.ts : 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}); 
       });

Under the config part i ve mocked my RouteNavigator service like the following : 在配置部分下,我模拟了RouteNavigator服务,如下所示:

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

My test fails throwing tghis error : 我的测试无法引发tghis错误:

TypeError: this.router.myMethod is not a function TypeError:this.router.myMethod不是函数

The log file says that the problem points on 98:18 which is exactly this line : 日志文件说问题指向98:18正是这一行:

comp.goToPrevious(); comp.goToPrevious();

Therefore goToPrevious() is implemented in my component like this : 因此, goToPrevious()在我的组件中实现为:

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

And routeNavigator refers to a custom service used to deal with custom redirection , like this : 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;
      }
     }

Any ideas about what's the problem or how to deal whith it ?? 关于问题是什么或如何解决的任何想法?

Because in goToPrevious() you use this.router.myMethod(); 因为在goToPrevious()使用this.router.myMethod();

Unless you expanded the router, I don't have knowledge of this in the Router module of Angular ! 除非您扩展了路由器,否则我在Angular的路由器模块中对此一无所知!

EDIT your mock should contain this : 编辑您的模拟应包含以下内容:

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