【问题标题】:How to spy a "t" function in react-i18next?如何在 react-i18next 中监视“t”函数?
【发布时间】:2021-12-24 17:20:35
【问题描述】:

我需要测试这个场景:

  • 在组件中:
t("some.key", { name: "Ash" })
  • 在 json 中:
{ "some": { "key": "Hi {{name}}" }

我想监视“t”函数并在测试中获取传递了哪些参数以及返回了什么。

有些是这样的:

expect(spyed).toHaveBeenCaledWith("some.key", { name: "Ash" })
expect(spyed).toHaveReturnedWith("Hi Ash")

【问题讨论】:

    标签: reactjs unit-testing jestjs react-testing-library i18next


    【解决方案1】:

    通常,对实现细节做出断言是一种不好的做法。

    i18next 作为一种特殊模式,称为cimode,这将使您的t 函数返回密钥本身。这允许您的测试对您的操作结果做出断言(检查元素是否包含键值)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 2017-10-20
      • 2020-02-15
      相关资源
      最近更新 更多