小编arw*_*dab的帖子

无效构造函数参数的自动混合测试

我有以下课程和考试.我想测试将null值作为参数传递给构造函数并期待一个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>();
}
Run Code Online (Sandbox Code Playgroud)

autofixture

11
推荐指数
1
解决办法
1257
查看次数

标签 统计

autofixture ×1