通常情況下,你測試,如果一個異常被拋出某種方法,如下所示。 我用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>();
}
但是如何測試,如果有異常被在構造函數拋出? 我只是這樣做,但是有沒有更合適的方法 通過FluentAssertions?
[Fact]
public void Constructor_throws_Exception()
{
// Arrange
Action a =() => new Foo(null); // null is an invalid argument
// Act/Assert
a.ShouldThrow<ArgumentNullException>();
}
我不知道庫,但我喜歡你所做的 –