Normally you test, if an exception gets thrown in a certain method, as follows. I use FluentAssertions:
[Fact]
public void Exception_gets_thrown()
{
// Arrange
var foo = new Foo("validArgument");
// Act/Assert
foo.Invoking(f => f.Bar(null)) // null is an invalid argument
.ShouldThrow<ArgumentNullException>();
}
But how to test, if an exception gets thrown in the constructor? I just did it like this, but is there maybe a more appropriate way via FluentAssertions?
[Fact]
public void Constructor_throws_Exception()
{
// Arrange
Action a = () => new Foo(null); // null is an invalid argument
// Act/Assert
a.ShouldThrow<ArgumentNullException>();
}