I have the following class and test. I want to test passing a null value as a parameter to the constructor and are expecting an ArgumentNullException
. But since I use the Autofixture's CreateAnonymous
method I get a TargetInvocationException
instead.
What is the correct way to write those kinds of tests?
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>();
}
ExpectedExceptionAttribute
is a bad idea - if you can't use xUnit at least use some form ofAssert.Throws
(Yes I know neither of these confront your question regardingTargetInvocationException
which is why this isnt an Answer, but step one is to move to clarify the code so you can work with it - even if this is a bug, you'll want to produce a clearer repro and a workaround for the interim – Welcher