Elmor
Elmor

Reputation: 4895

rspec have_xml fails but error message shows presence of element

I'm in a bit of trouble - can't understand why my rspec test started failing and can't find xml. It's implemented using this . Could anyone help me solve this issue?
Rspec test:

response.status.should == 200
response.body.should have_xml('/PhoneBook/PhoneBookEntries/Contact/Id', @new_contact.id.to_s)

And output of the console:

   Failure/Error: response.body.should have_xml('/PhoneBook/PhoneBookEntries/Contact/Id', @new_contact.id.to_s)
       expected to find xml tag /PhoneBook/PhoneBookEntries/Contact/Id in:
       <PhoneBook>
         <APIVersion>1.0</APIVersion>
         <SyncTime>1377192796</SyncTime>
         <PhoneBookEntries>
           <Contact>
             <Id>182</Id>
             <Avatar>
               <type>PNG</type>
               <data>iVBORw0KGgoAAAANSUhEUgAAAKAAAACgAQAAAACjtFqAAAAABGdBTUEAALGP
       C/xhBQAAAAFzUkdCAK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUw
       AADqYAAAOpgAABdwnLpRPAAAAAJiS0dEAAHdihOkAAAACXBIWXMAAABIAAAA
       SABGyWs+AAAAIklEQVRIx+3IMQEAAAwCIPuX1gJrMDhJD5FSSimllFLKfznP
       BnQ17b9ZHAAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAxMy0wOC0yMlQyMDozMzox
       NiswMzowMDdI2UsAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMTMtMDgtMjJUMjA6
       MzM6MTYrMDM6MDBGFWH3AAAAAElFTkSuQmCC
       </data>
               <Modified>1377192796</Modified>
             </Avatar>
           </Contact>
           <Contact>
             <Id>183</Id>
             <Avatar>
               <type>PNG</type>
               <data>iVBORw0KGgoAAAANSUhEUgAAAKAAAACgAQAAAACjtFqAAAAABGdBTUEAALGP
       C/xhBQAAAAFzUkdCAK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUw
       AADqYAAAOpgAABdwnLpRPAAAAAJiS0dEAAHdihOkAAAACXBIWXMAAABIAAAA
       SABGyWs+AAAAIklEQVRIx+3IMQEAAAwCIPuX1gJrMDhJD5FSSimllFLKfznP
       BnQ17b9ZHAAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAxMy0wOC0yMlQyMDozMzox
       NiswMzowMDdI2UsAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMTMtMDgtMjJUMjA6
       MzM6MTYrMDM6MDBGFWH3AAAAAElFTkSuQmCC
       </data>
               <Modified>1345656796</Modified>
             </Avatar>
           </Contact>
         </PhoneBookEntries>
       </PhoneBook>

Upvotes: 0

Views: 250

Answers (1)

Neil Slater
Neil Slater

Reputation: 27207

The matcher you linked code for cannot cope with two matching nodes having different text values. Or, more accurately, it will always fail to match if you give it an xpath that selects two or more nodes with different text.

Your XML has two matching nodes for xpath /PhoneBook/PhoneBookEntries/Contact/Id, these matches will not both have text containing the same string id. That is why you get a match failure.

Probably the simplest thing to do without fixing the matcher is select only one node to match in the xpath, and rely on the behaviour when not supplying a second text param

response.body.should have_xml( 
   "/PhoneBook/PhoneBookEntries/Contact[Id='#{@new_contact.id}']" )

This moves the match to Id into the xpath query itself, so getting a matching node means you've found the Id you want to assert has been added. The xpath selects the matching Contact node, not the Id node, which is something you might find useful for further assertions.

Upvotes: 1

Related Questions