Skip to content

HaveElement expects empty namespace #1523

@stilettk

Description

@stilettk

Description

HaveElement() expects an element with a given name and without any namespace, instead of checking for name only.

Complete minimal example reproducing the issue

const string xml = @"<root xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""test""><child>value</child></root>";
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
var xmlElement = xmlDoc.DocumentElement;

xmlElement["child"].Should().NotBeNull(); // passes
xmlElement.Should().HaveElement("child"); // fails, because internally it checks for xmlElement["child", ""]

Expected behavior:

Test passes.

Actual behavior:

Test fails on HaveElement, while xmlElement["child"] is not null.

Versions

FluentAssertions v5.10.3
.Net Core 3.1

Additional Information

This is because in implementation, the code uses ToHaveElementWithNamespace and passes string.Empty as expectedNamespace.
https://github.com/fluentassertions/fluentassertions/blob/develop/Src/FluentAssertions/Xml/XmlElementAssertions.cs#L131

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions