如何将复杂的对象作为模拟数据服务存根传递

发布于 2025-02-11 00:40:50 字数 1217 浏览 1 评论 0原文

我正在研究一个模块的测试,除了我无法访问的事实外,它是内在对象属性属性的事实。

tokenStorageServiceStub = { // Mock service data
    getUser: () => of({
        username: 'organization-a-a-normal',
        roles: ['PROCESSOR'],
        attributes: {
            organizationId: 111,
            organizationName: 'Org.name Aa',
            userFullName: 'User for organization A-A',
            userId: 22,
        },
        access_token: 'AT01',
        refresh_token: 'RT01',
        token_type: 'Bearer',
        expires_in: 3600,
    }),
};

组件代码抱怨最后一个语句:

this.activeTokens = this.token.getUser(); // OK
this.username = this.activeTokens.username; // OK
this.organization.id = this.activeTokens.attributes.organizationId; // `attributes` is undefined?

Chrome无头88.0.4298.0(Linux X86_64)settingscomponent应该创建失败
TypeError:无法阅读未定义的属性' 在settingscomponent.call [as ngoninit](src/app/app/andertion/settings/settings.component.ts:22:61)

我认为这是因为 rxjs.s.of()函数的内部工作。在手册中,我看到了类似可观察的信息。但是没有提及这样的对象。

我还阅读了有关()被弃用的吗?在测试服务存根中传递对象数据的好方法是什么?

I'm working on a test for a module, which works except for the fact that I can not access, the inner object attributes properties.

tokenStorageServiceStub = { // Mock service data
    getUser: () => of({
        username: 'organization-a-a-normal',
        roles: ['PROCESSOR'],
        attributes: {
            organizationId: 111,
            organizationName: 'Org.name Aa',
            userFullName: 'User for organization A-A',
            userId: 22,
        },
        access_token: 'AT01',
        refresh_token: 'RT01',
        token_type: 'Bearer',
        expires_in: 3600,
    }),
};

The component code complains about the last statement:

this.activeTokens = this.token.getUser(); // OK
this.username = this.activeTokens.username; // OK
this.organization.id = this.activeTokens.attributes.organizationId; // `attributes` is undefined?

Chrome Headless 88.0.4298.0 (Linux x86_64) SettingsComponent should create FAILED
TypeError: Cannot read property 'organizationId' of undefined
at SettingsComponent.call [as ngOnInit] (src/app/organization/settings/settings.component.ts:22:61)

I think it's because of the innerworking of the RxJs.of() function. In the manual, I see things like ObservableInput. But there is no mention of Objects as such.

I also read about of() getting deprecated? What will be a good approach to pass my object data in a test service stub?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文