Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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
Angular 用茉莉花测试角质栎属植物_Angular_Unit Testing_Jasmine_Karma Jasmine - Fatal编程技术网

Angular 用茉莉花测试角质栎属植物

Angular 用茉莉花测试角质栎属植物,angular,unit-testing,jasmine,karma-jasmine,Angular,Unit Testing,Jasmine,Karma Jasmine,我希望能够测试从一个视图传递到另一个视图的参数。我想测试看看参数是否存在,以及参数是否与我给出的模拟测试数据相匹配 我对单元测试还很陌生,对设置关于激活路由的测试和传递参数做了大量阅读。我想我一直坚持的是“期望”。这给了我一个错误 Argument of type '{ urn: string[]; }' is not assignable to parameter of type 'Expected<Observable<Params>>' 规格 providers:

我希望能够测试从一个视图传递到另一个视图的参数。我想测试看看参数是否存在,以及参数是否与我给出的模拟测试数据相匹配

我对单元测试还很陌生,对设置关于激活路由的测试和传递参数做了大量阅读。我想我一直坚持的是“期望”。这给了我一个错误

Argument of type '{ urn: string[]; }' is not assignable to parameter of type 'Expected<Observable<Params>>'
规格

providers: [
        HttpClient,
        {
          provide: ActivatedRoute,
          useValue: {
            queryParams: of({
              urn: '123'
            })
          }
        }
      ],
...

  it('test queryParam in route', () => {
    const activatedRoute: ActivatedRoute = fixture.debugElement.injector.get(ActivatedRoute);

    activatedRoute.queryParams = of({ urn: '123' });

    fixture.detectChanges(); 
    //  tick();

    expect(activatedRoute.queryParams).toEqual({ urn: ['123'] }); // this line giving me trouble
  });
如果有人能帮我看看我做错了什么-这里是

expect(activatedRoute.queryParams).toEqual({ urn: ['123'] })
activatedRoute.queryParams
不是
{urn:['123']}
,而是一个将触发此值的
可观察的

您可以这样测试它:

  /*
    Notice the extra "done" parameter. It is a function that you must call
    to indicate that the test is finished.
    It is necessary because we are testing an asynchronous method.
    This will prevent the test from exiting until the "done" method is called.
    Also, the test will fail if the done method is not called within 5s by default.
  */                                
  it('test queryParam in route', (done) => {
    [...]

    activatedRoute.queryParams.subscribe((value) => {
      expect(value).toEqual({ urn: ['123'] })
      done();
    })

  });

谢谢-我不知道“done”参数。我希望其他人发现这个问题和答案有用。我得到的唯一问题是“TypeError:无法读取未定义的属性'subscribe'”——但我可以看到.subscribe中的值是相等的。@TomRudge如果
activatedRoute.queryParams
未定义的
,您将无法到达回调的内部。我猜这个错误来自其他地方。您是否有行号或其他帮助您查找此错误来源的信息?它是来自
.ts
还是来自
spec.ts
?它与@Arnaud无关,但您仍然可以提供帮助?
  /*
    Notice the extra "done" parameter. It is a function that you must call
    to indicate that the test is finished.
    It is necessary because we are testing an asynchronous method.
    This will prevent the test from exiting until the "done" method is called.
    Also, the test will fail if the done method is not called within 5s by default.
  */                                
  it('test queryParam in route', (done) => {
    [...]

    activatedRoute.queryParams.subscribe((value) => {
      expect(value).toEqual({ urn: ['123'] })
      done();
    })

  });