RSpec / Rails模板渲染匹配不正确? (“_ x”匹配“_xy”?)

时间:2011-12-11 23:49:17

标签: ruby-on-rails rspec ruby-on-rails-3.1 rspec-rails

或者我误解了什么?

部分生活在:

app/views/admin/command_templates/_fillerup.html.erb

此规范通过(视图目录中没有“填充”或“_fill”):

describe "GET fillerup" do
  it "assigns some stuff and renders a partial" do
    should render_template "admin/command_templates/_fill"     # I expected failure
    should render_template "admin/command_templates/_fillerup" # "Correct" test
  end
end

这失败了(正如我所料):

describe "GET fillerup" do
  it "assigns some stuff and renders a partial" do
    should render_template "admin/command_templates/_fillerupp" # extra "p"
  end
end

行为就像在路径上做start_with?一样,可能忽略扩展,或者......?

rspec-rails 2.7.0,rails 3.1.2;其他信息可根据要求提供。

验证行为存在(或不存在)将有助于进行健全性检查。

1 个答案:

答案 0 :(得分:2)

查看render_template的代码 - 其中许多方法通过正则表达式进行模式匹配,这与starts_with?类似,至少与模式“Joe”相同将匹配字符串“乔史密斯”和“乔空白”。我不是在这里做模式匹配正义,但这只是一个例子。

模式匹配这种方式主要是为了方便,正如我所理解的那样,因此您不必在测试中过于具体,这会使您的测试框架变得不那么脆弱。您通常可以放心使用它而不必担心,因为模板通常没有相似的名称。

如果您只需要匹配特定模板(而不是其他模板),则可以添加其他条件测试,例如:

should render_template('x')
should_not render_template('xy')

...或类似的东西,它应该有效地过滤掉以相似名称开头但不是你感兴趣的模板的模板。