Question

How to assert the multiline string with RSpec? I've something like below as expectation:

it "should generate the correct header for the xml file" do
  xml_header = <<-XML
  <?xml version="1.0" encoding="utf-8"?>
  <FileSpec>
    <Header>
        <Version>1.0</Version>
        <DateTime>2010-10-17T10:41:18</DateTime>
        <Sequence>3</Sequence>
        <ClientID>QQWW</ClientID>
        <FileType>QQWW</FileType>
    </Header>
  </FileSpec>
  XML
  puts SomeXml.build_xml_header
  SomeXml.build_xml_header.to_s.should == xml_header
end

Though the output is as expected, the test fails due to the new-lines and tabs characters. How to assert in such condition? I think that == matcher cant handle it.

Failure/Error: SomeXml.build_xml_header.to_s.should == xml_header
 expected: "    <?xml version=\"1.0\" encoding=\"utf-8\"?>\n    <FileSpec>\n    \t<Header>\n    \t\t<Version>1.0</Version>\n    \t\t<DateTime>2010-10-17T10:41:18</DateTime>\n    \t\t<Sequence>3</Sequence>\n    \t\t<ClientID>QQWW</ClientID>\n    \t\t<FileType>QQWW</FileType>\n    \t</Header>\n    </FileSpec>\n",
      got: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<FileSpec>\n  <Header>\n    <Version>1.0</Version>\n    <DateTime>2010-10-17T10:41:18</DateTime>\n    <Sequence>3</Sequence>\n    <ClientID>QQWW</ClientID>\n    <FileType>QQWW</FileType>\n  </Header>\n</FileSpec>\n" (using ==)
Was it helpful?

Solution

Edit: I didn't look at the XML as closely as I should have. It appears your whitespace and string cases don't match. One has UTF-8 and the other has utf-8. If you strip out the whitespace and make the search case insensitive, it should pass.

There's probably a better way to do this match in rspec, but for simplicity:

a.gsub(/\s/,'').downcase.should == b.gsub(/\s/,'').downcase
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top