0

如果我在下面有这个模拟激活路由,我可以在类型为“不匹配”时编写一些测试,但如果我想编写一些类型为“管理员不匹配”的测试。如何更改查询参数的值?

        await TestBed.configureTestingModule({
            declarations: [ErrorPageComponent],
            imports: [GraphQLModule, RouterTestingModule],
            providers: [
                Apollo,
                {
                    provide: ActivatedRoute,
                    useValue: {
                        queryParams: of({
                            name: 'John Doe',
                            cacheKey: 'cacheKey',
                            type: 'mismatch'
                        })
                    }
                },
            ]
        }).compileComponents();
    });


    beforeEach(() => {
        fixture = TestBed.createComponent(ErrorPageComponent);
        component = fixture.componentInstance;
        route = TestBed.inject(ActivatedRoute);
        fixture.detectChanges();
    });
4

1 回答 1

0

您可以使用Subjector BehaviorSubject,然后您可以控制发出的内容:

describe('', () => {
  const activatedRouteQueryParams = new BehaviorSubject({
    name: 'John Doe',
    cacheKey: 'cacheKey',
    type: 'mismatch'
  });

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [ErrorPageComponent],
      imports: [GraphQLModule, RouterTestingModule],
      providers: [
        Apollo,
        {
          provide: ActivatedRoute,
          useValue: {
            queryParams: activatedRouteQueryParams,
          }
        },
      ]
    }).compileComponents();
  });

  it('test', () => {
    activatedRouteQueryParams.next({
      name: 'John Doe',
      cacheKey: 'cacheKey',
      type: 'admin-mismatch',
    });
  });
于 2021-07-19T18:17:52.743 回答