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

60 views Asked by At
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.

2

There are 2 answers

1
koffeinfrei On BEST ANSWER

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
0
challeng On

I don't see why that would not work using the code you provided. Maybe use include? instead. You also could use a regex to determine if it was HTML. While for this specific example it might be a little much, you may be able to determine more specific things about the html then just if it has that tag.