0

I would like to use fluent assertions .Should().Throw, but do not want to hardcode the type of the expected exception, instead I want it parametrized, so it is in a variable which type is Type.

Without parametrized it should be the following:

Action act = () => sut.MyMethod();
act.Should().Throw<MyException>().Where(myPredicate);

However I would like to the type of MyException to be a parameter in a variable

g.pickardou
  • 32,346
  • 36
  • 123
  • 268

1 Answers1

1

FluentAssertion's API is generation. So, currently you must provide at least the base class of all exception: Exception. Then, you can validate the type using BeOfType.

act.Should().Throw<Exception>().Which.Should().BeOfType(typeof(MyException));
lg2de
  • 616
  • 4
  • 16
  • @g.pickardou, would be great if you accept the answer if it is the solution for your problem! – lg2de Jul 08 '23 at 10:03