Search code examples
rubyrspecexpectations

What is a good way to test a method that expects an HTML string?


foo.should_receive( :save ).with( html )

Where html is an HTML string, but I don't want to check against specific HTML because that would make the test brittle.

Is there a way to check the length of the param that foo should receive? Is it possible to use a matcher or something like html.should include '<html'?

Working in RSpec.


Solution

  • As stated in the comment above you can use a regex for the argument matcher.

    foo.should_receive(:save).with(/<html/)
    

    If you want to do more complicated assertions you can provide a block:

    foo.should_receive(:save).with do |arg|
      arg.should include '<html'
    end