【问题标题】:Angular Jasmine test case for navigation and getCurrentNavigation not working用于导航和 getCurrentNavigation 的 Angular Jasmine 测试用例不起作用
【发布时间】:2021-10-15 18:11:57
【问题描述】:

我的组件同时具有getCurrentNavigationrouter.navigate

我已经从here实现了getCurrentNavigation

现在页面有

constructor() {
 if (router.getCurrentNavigation().extras.state) {
      this.levelId = router.getCurrentNavigation().extras.state.arcadeSelectedGame;
    }
}

并导航到另一个页面

const navigationExtras: NavigationExtras = {
        state: {
          dummyObject,
          gameplay_log_data,
          arcadeLevelData: this.arcadeLevelData
        }
      };
 this.router.navigate(['ad-page'], navigationExtras)

现在我已经应用了 here 的解决方案,然后导航工作,但随后 router.getCurrentNavigation().extras.state 停止工作

【问题讨论】:

    标签: angular ionic-framework karma-jasmine angular-test


    【解决方案1】:

    我找到了一个值来实现这两种情况的相似之处。 我已经用getCurrentNavigation 定义了 navigate: jasmine.createSpy('navigate')。并使用useValue 代替useClass

    这里是代码。

    let mockRouter = {
        navigate: jasmine.createSpy('navigate'),
        getCurrentNavigation: () => {
          return {
             extras: {
                state:{
                  arcadeSelectedGame: '-LAbVp7C0lTu9CV-Mgt-'
                }
              }
            }
          }
      }
    

    在提供者数组中

    { 提供:路由器,使用值:模拟路由器},

    let router: Router;
    

    foreach 内部

    router = TestBed.inject(Router);
    

    测试用例

    it('Play click check navigation', async () => {
       component.playArcade();
       expect(mockRouter.navigate).toHaveBeenCalledTimes(1)
       expect(mockRouter.navigate).toHaveBeenCalledWith(['ad-page']);
    })
    

    【讨论】:

      猜你喜欢
      • 2017-05-25
      • 2017-04-15
      • 2020-08-21
      • 1970-01-01
      • 2016-05-14
      • 2018-01-22
      • 1970-01-01
      • 2020-09-04
      • 2020-11-20
      相关资源
      最近更新 更多