Angular2 RC5模拟激活的路线参数 [英] Angular2 RC5 Mock Activated Route Params

查看:90
本文介绍了Angular2 RC5模拟激活的路线参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够模拟激活的路由参数,以便能够测试我的组件。

I need to be able to mock the activated route parameters to be able to test my component.

到目前为止,这是我最好的尝试,但是没有用。

Here's my best attempt so far, but it doesn't work.

{ provide: ActivatedRoute, useValue: { params: [ { 'id': 1 } ] } },

ActivatedRoute用于实际组件,如下所示:

The ActivatedRoute is used in the actual component like this:

this.route.params.subscribe(params => {
    this.stateId = +params['id'];

    this.stateService.getState(this.stateId).then(state => {
        this.state = state;
    });
});

我目前尝试的错误很简单:

The error I get with my current attempt is simply:

TypeError:未定义不是构造函数(正在评估'this.route.params.subscribe')

任何帮助将不胜感激。

推荐答案

您的模拟必须反映要替换的对象。您 .subscribe 是因为它返回一个可观察的对象,而不仅仅是对象,因此您的模拟值也应:

Your mock must reflect the object it's replacing. You .subscribe because it returns an observable, not just the object, so your mock value should too:

import { Observable } from 'rxjs/Rx';

...

{ provide: ActivatedRoute, useValue: { 'params': Observable.from([{ 'id': 1 }]) } }

这篇关于Angular2 RC5模拟激活的路线参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆