I want to test that an XML element's value contains a particular string. [The code below is clearly highly contrived and not from the real codebase I am working on]. How to check with Fluent Assertions that the XElement's Value contains a string?
using System.Xml.Linq;
using FluentAssertions;
class Program
{
private static void Main()
{
var x = new XElement("Root", new XElement("Name", "Fat Cat Sat"));
x.Should().HaveElement("Name").Which.Should().HaveValue("Fat Cat Sat");
// don't know how to do this:
x.Should().HaveElement("Name").Which.Should().HaveValueContaining("Cat");
}
}
The value of your XML node is just a string, having multiple "entries" separated by spaces isn't anything special in XML - so we need to treat this assertion as if it's a normal string.
The important difference here is
.Which.Value
, so that we are only asserting on the value of the node (which is a string), rather than the entire node. From there, you can split by spaces, then check if the resulting collection contains one of the entries you're looking for.