1

Closed

HaveElement doesn't seem to work

description

I have a class the produces the following XML...

<Operation Name="Test" Duration="0" />

If I parse the above XML into an XDocument and try to assert like so...

var testDoc = XDocument.Parse(result);
testDoc.Should().HaveElement("Operation");

I get the following error...

Expected XML document <Operation Name=\"Test\" Duration=\"1\" /> to have root element with child <Operation>, but no such child element was found.

If I try parsing it as an XElement like so...

var testElement = XElement.Parse(result);
testElement.Should().HaveElement("Operation");

I get the same error of...

Expected XML element <Operation Name=\"Test\" Duration=\"0\" /> to have child element <Operation>, but no such child element was found.
Closed Aug 24, 2012 at 6:05 AM by
Since you've found the error yourself....

comments

Oliwa wrote Aug 21, 2012 at 7:39 PM

I found the fix. Use HaveRoot on the XDocument like so...

var testDoc = XDocument.Parse(result);
testDoc.Should().HaveRoot("Operation");