4

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.

Stuart Hemming
  • 1,553
  • 2
  • 21
  • 44

2 Answers2

5

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

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

Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44
2

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);
jBelanger
  • 1,526
  • 18
  • 11