如何正确测试 yaml 文件中的内插字符串

How to properly test interpolated string from yaml file

我有 rspec/capybara 测试正在测试字符串的期望值,但测试正在替换变量。

yaml 中的刺痛:

title: Sold %{product}

测试期望:

expect(page).to have_text(t("sell_successes.show.title"))

结果:

expected to find text "Sold %{product}" in blah blah

应该在什么时候寻找:

expected to find text "Sold shoes" in blah blah

yaml 包含一个字符串模板,因此您需要为其提供要放入模板的值

expect(page).to have_text(t("sell_successes.show.title") % { product: 'shoes' })