【发布时间】:2013-02-11 06:30:48
【问题描述】:
我有以下课程和测试。我想测试将空值作为参数传递给构造函数,并期待ArgumentNullException。但由于我使用 Autofixture 的 CreateAnonymous 方法,我得到了 TargetInvocationException 。
编写这些测试的正确方法是什么?
public sealed class CreateObject : Command {
// Properties
public ObjectId[] Ids { get; private set; }
public ObjectTypeId ObjectType { get; private set; }
public UserId CreatedBy { get; private set; }
// Constructor
public CreateObject(ObjectId[] ids, ObjectTypeId objectType, UserId createdBy) {
Guard.NotNull(ids, "ids");
Guard.NotNull(objectType, "objectType");
Guard.NotNull(createdBy, "createdBy");
Ids = ids;
ObjectType = objectType;
CreatedBy = createdBy;
}
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void constructor_with_null_ids_throw() {
fixture.Register<ObjectId[]>(() => null);
fixture.CreateAnonymous<CreateObject>();
}
【问题讨论】:
-
我会在更高级别进行此类 Guard 子句验证 - 请参阅 stackoverflow.com/a/11455580/11635。另外,@ 987654322@(是的,我知道这些都没有遇到您关于@ 987654329@的问题,这就是为什么这不是答案,但第一步是澄清代码以便您可以使用它 - 即使这是一个错误,您将需要生成更清晰的重现和临时解决方法
标签: autofixture