Stuart Hemming
Stuart Hemming

Reputation: 1663

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.

Upvotes: 4

Views: 1692

Answers (2)

jBelanger
jBelanger

Reputation: 1778

One simple solution is to use the AndWhichConstraint type returned by BeOfType<>().

This is what I end up doing:

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

Upvotes: 2

Dennis Doomen
Dennis Doomen

Reputation: 8889

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

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

Upvotes: 6

Related Questions