views:

429

answers:

4

I'm using the following webrat matcher:

response.should contain(text)

With the following haml:

%p
  You have
  = current_user.credits
  credits

I've written the cucumber step 'Then I should see "You have 10 credits"', which uses the webrat matcher above. The step fails, webrat does not find the text in the response because the haml actually produces

<p>You have
10
credits</p>

How can I get the matcher to match the output that haml produces?

Note: the above is a simplified example to the situation i'm dealing with. Writing the following haml is not an acceptable solution:

%p= "You have #{current_user.credits} credits"
+3  A: 

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.)

SFEley
Your multiline Haml doesn't work. The multiline operator is identical to putting everything on the same line, so your example is equivalent to `You have = current_user.credits`. The `=` just becomes part of the text.
nex3
+1  A: 

I've found that something like:

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

will often give me the match I want without me having to goof with my Haml. Given the choice between mucking with my markup, which I really want to be readable, and changing my matcher to a regular expression, the latter seems a small price to pay for the more straightforward coding of the view.

Steve Ross
+1  A: 

Better than

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

would be

%p You have #{current_user.credits} credits

since Haml automatically interpolates text nodes.

Stephen Touset
A: 

There are various facilities in Haml for manipulating whitespace, but the correct thing to do here is to either revise the matcher to be whitespace-independent or to use a filter for writing your inline content. For example:

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

Or if you need more complex logic:

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

Haml is designed for efficiently expressing the structure of a document, but it's not so good at expressing inline content. When you want to do fancy inline stuff (as here), it makes sense to drop into ERB/HTML rather than going purely Haml. For more details see this blog post.

nex3