Dev
Dev

Reputation: 1541

Fluent assertions for xml

I have a xml file of the form :

<Level1>
   <Level2>
        <Level3>
             <Level4 attr1 = "123.4" attr2 = ""> </Level4>
        </Level3>
   </Level2>
<Level1> 

I'm using XUnit to check the structure of the xml.

[Fact]
public void Xml_Check()
{
    var doc = XDocument.Load("test.xml");
    doc.Should().HaveRoot("Level1");
    doc.Should().HaveElement("Level2");
    doc.Should().HaveElement("Level3");  //Erroring on this line
}

I'm getting the error: Expected XML document <Level1>...</Level1> to have root element with child "Level3" but no such child element was found. It is trying to treat Level3 as a child of Level1 instead of Level2.

How do I get the Level3 and check whether certain attributes exist in Level4? Is there a way to check the type of the attribute value?

Upvotes: 3

Views: 1330

Answers (1)

Dev
Dev

Reputation: 1541

Finally got it working and someone might find it useful in future

It should be:

doc.Should().HaveElement("Level2").Which.Should().HaveElement("Level3");

Upvotes: 5

Related Questions