Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing assertions on multiple properties of a Class

I know from the docs I can do this ...

result.Should().BeOfType<MyClass>().Which.Property1.Should().Be("String")

Is there a way I can test multiple properties, in a way similar

result.Should().BeOfType<MyClass>().Which.Property1.Should().Be("String").And.Property2.Should().Be(99);

It's also be good if it were possible to do either of the above tests without having to assert hat they are 'OfType' but I suspect that there is no other way for the code to know what properties are available.

like image 860
Stuart Hemming Avatar asked Sep 08 '25 01:09

Stuart Hemming


1 Answers

You can do a structural comparison assertion against an anonymous type, like this:

result.ShouldBeEquivalentTo(new { Property1 = "String", Property2 = 99 }, options => options.ExcludingMissingMembers());

like image 178
Dennis Doomen Avatar answered Sep 11 '25 00:09

Dennis Doomen