Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度:如何在角度单元测试中使用路由器_Javascript_Angular_Typescript_Unit Testing_Karma Jasmine - Fatal编程技术网

Javascript 角度:如何在角度单元测试中使用路由器

Javascript 角度:如何在角度单元测试中使用路由器,javascript,angular,typescript,unit-testing,karma-jasmine,Javascript,Angular,Typescript,Unit Testing,Karma Jasmine,我正在用angular编写一个测试用例。如果res.length==1重定向到详细信息页面,我已经编写了一个条件。(this.router.navigate(['details',id]))。我从body响应中对象的第一个数组中获取id,作为const id=res[0]。id。这两行代码都不在我的代码覆盖范围内。有人能告诉我哪里出错了吗 我被[['/product details',SAAASD001']]调用了预期spy navigate,但从未调用过它。 应用组件规范ts let rout

我正在用angular编写一个测试用例。如果
res.length==1
重定向到详细信息页面,我已经编写了一个条件。(
this.router.navigate(['details',id])
)。我从body响应中对象的第一个数组中获取
id
,作为
const id=res[0]。id
。这两行代码都不在我的代码覆盖范围内。有人能告诉我哪里出错了吗

我被[['/product details',SAAASD001']]调用了
预期spy navigate,但从未调用过它。

应用组件规范ts

let router = {navigate: jasmine.createSpy('navigate')};
  getList() {
    this.store
      .select('content', 'catalogue')
      .pipe(takeUntil(this.onDestroy$))
      .subscribe((res) => {
        Iif (res.length === 1) {
          // this line doesn't cover
          const id = res[0].id;
          // this line doesn't cover
          this.router.navigate(['details', id]);
        } else {
          this.list = category(res);
        }
      });
  }
应用程序组件.ts

let router = {navigate: jasmine.createSpy('navigate')};
  getList() {
    this.store
      .select('content', 'catalogue')
      .pipe(takeUntil(this.onDestroy$))
      .subscribe((res) => {
        Iif (res.length === 1) {
          // this line doesn't cover
          const id = res[0].id;
          // this line doesn't cover
          this.router.navigate(['details', id]);
        } else {
          this.list = category(res);
        }
      });
  }

路由器
存储
在组件中公开,然后尝试:

TestBed.configureTestingModule({
  imports: [RouterTestingModule],
  providers: [Router,Store ]
})

it('should take data from store', () => {
  const response = [{id: 'val'}];
  spyOn(component.store,"select").and.returnValue(of(response));
  spyOn(component.router,"navigate").and.callThrough();
  component.getList();
  expect(router.navigate).toHaveBeenCalledWith(['/details', response[0].id]);
});

同样地,通过更改
const response=[{id:'val'}]来覆盖
else
部分

将此内容添加到导入中:

 RouterTestingModule.withRoutes([
          { path: 'path1', component: TestComponent1},
          { path: 'home', component: DashboardComponent }
        ]),
稍后在您的测试用例中:您可以监视路由器的“导航”,并使用它重定向到导入中提到的任何组件

spyOn(Router,"navigate").and.callthrough();
router.navigate([/path1]);

你能发布
routerSpy
的声明吗?@alexortizl抱歉。更新。它应该是
router
什么时候调用getList()方法?你似乎错过了()@satchcoder,嗯,这不是问题。问题是我如何在测试中涵盖路由器代码:(