rubyxmlrspecchef-infrachefspec

How do I mock a XML file in RSpec


I have an XML file hw.xml that contains hardware info for a node.

<node hostname="my_hostname">
  <volume raid="RAID-10">
   ....
   ....
  </volume>
</node> 

how to mock the xml file such that it should fail the test cases if raid attribute is not equal to RAID-10?


Solution

  • You can create a XML mock using Nokogiri, like this:

    builder = Nokogiri::XML::Builder.new do |xml|
      xml.root {
        xml.products {
          xml.widget {
            xml.id_ "10"
            xml.name "Awesome widget"
          }
        }
      }
    end
    puts builder.to_xml
    

    You can see the documentation for this here.