如何从yaml文件中正确测试插值字符串

时间:2018-06-15 17:39:36

标签: ruby-on-rails rspec yaml capybara

我有测试字符串期望的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

1 个答案:

答案 0 :(得分:1)

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

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