简体   繁体   English

如何定义中继的默认模拟数据?

[英]How to define default mock data for relay?

I am doing unit tests on a react app that is using relay as a graphql client when testing , i get mocked data with like this :我正在对一个在测试时使用中继作为graphql客户端的反应应用程序进行单元测试,我得到这样的模拟数据:

        title: '<mock-value-for-field-"title">',

How can i define default mock data ?如何定义默认模拟数据?

To mock the specific value of a field in Relay you will want to use MockPayloadGenerator .要模拟 Relay 中某个字段的特定值,您将需要使用MockPayloadGenerator

https://relay.dev/docs/guides/testing-relay-components/#mock-payload-generator-and-the-relay_test_operation-directive https://relay.dev/docs/guides/testing-relay-components/#mock-payload-generator-and-the-relay_test_operation-directive

Will need more code to give a complete example but essentially will need to do something similar to:需要更多代码来给出一个完整的例子,但本质上需要做一些类似的事情:

    environment.mock.resolveMostRecentOperation(operation =>
      MockPayloadGenerator.generate(operation, {
        String(context) {
          if (context.name === 'title') {
            return 'value you want to be used for title field'
          }
        },
      }),
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM