【发布时间】:2012-06-09 15:38:30
【问题描述】:
通常你会测试,如果在某个方法中抛出异常,如下所示。 我使用 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>();
}
【问题讨论】:
标签: c# .net unit-testing xunit.net fluent-assertions