R – Using webrat’s contain(text) matcher with haml

cucumberhamlruby-on-railswebrat

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"

Best Answer

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

Related Topic