2

我正在使用以下 webrat 匹配器:

response.should contain(text)

使用以下哈姆:

%p
  You have
  = current_user.credits
  credits

我已经编写了黄瓜步骤'然后我应该看到“你有 10 个学分”',它使用了上面的 webrat 匹配器。步骤失败,webrat 没有在响应中找到文本,因为 haml 实际产生

<p>You have
10
credits</p>

如何让匹配器匹配 haml 产生的输出?

注意:以上是我正在处理的情况的简化示例。编写以下 haml 不是可接受的解决方案:

%p= "You have #{current_user.credits} credits"
4

4 回答 4

3

You're right, this is a pain. I've found Webrat to be annoyingly touchy too much of the time.

Two ideas:

  1. Fix your test. In this case you want it to be blind to newlines, so get rid of them all: response.tr("\n","").should contain(text)
  2. Fix your Haml. This is probably the better option. You can use the multiline terminator | to tell Haml not to put line breaks in:
    %p
      You have |
      = current_user.credits |
      credits

See the Haml reference for more obscure stuff like this. (A surprising amount of which has to do with whitespace.)

于 2009-10-13T19:02:41.097 回答
2

优于

%p= "You have #{current_user.credits} credits"

将会

%p You have #{current_user.credits} credits

因为 Haml 会自动插入文本节点。

于 2010-10-26T16:18:32.407 回答
1

我发现了类似的东西:

response.should contain(/You have 10 credits/m)

经常会给我我想要的比赛,而我不必和我的 Haml 搞混。考虑到在使用我的标记(我真的希望它是可读的)和将我的匹配器更改为正则表达式之间进行选择,后者似乎为更直接的视图编码付出了很小的代价。

于 2009-11-13T22:37:53.917 回答
0

Haml 中有多种工具可以处理空白,但正确的做法是将匹配器修改为与空白无关,或者使用过滤器来编写内联内容。例如:

%p
  :plain
    You have #{current_user.credits} credits

或者,如果您需要更复杂的逻辑:

%p
  :erb
    You have <%= current_user.credits %> credits

Haml 旨在有效地表达文档的结构,但它并不擅长表达内联内容。当您想做花哨的内联内容时(如这里),使用 ERB/H​​TML 而不是纯粹的 Haml 是有意义的。有关更多详细信息,请参阅此博客文章

于 2010-10-26T18:15:32.147 回答