Skip to content

Latest commit

 

History

History
51 lines (39 loc) · 1.57 KB

xml.md

File metadata and controls

51 lines (39 loc) · 1.57 KB
title permalink layout classes sidebar
XML
/xml/
single
wide
nav
sidebar

Fluent Assertions has support for assertions on several of the LINQ-to-XML classes:

xDocument.Should().HaveRoot("configuration");
xDocument.Should().HaveElement("settings");
xDocument.Should().HaveElement("settings", Exactly.Once());
xDocument.Should().HaveElement("settings", AtLeast.Twice());

xElement.Should().HaveValue("36");
xElement.Should().HaveAttribute("age", "36");
xElement.Should().HaveElement("address");
xElement.Should().HaveElementWithNamespace("address", "http://www.example.com/2012/test");

xElement.Should().HaveInnerText("some textanother textmore text");

xElement.Should().HaveElement("settings", Exactly.Once());
xElement.Should().HaveElement("settings", AtLeast.Twice());

Those two last assertions also support XName parameters:

xElement.Should().HaveAttribute(XName.Get("age", "http://www.example.com/2012/test"), "36");
xElement.Should().HaveElement(XName.Get("address", "http://www.example.com/2012/test"));

xAttribute.Should().HaveValue("Amsterdam");

You can also perform a deep comparison between two elements like this.

xDocument.Should().BeEquivalentTo(XDocument.Parse("<configuration><item>value</item></configuration>"));
xElement.Should().BeEquivalentTo(XElement.Parse("<item>value</item>"));

Chaining additional assertions on top of a particular (root) element is possible through this syntax.

xDocument.Should().HaveElement("child")
  .Which.Should().BeOfType<XElement>()
    .And.HaveAttribute("attr", "1");