我正在尝试使用 Moq 来存根一个数据类,当我传递它的类型时,我想通过 Activator.CreateInstance 动态创建它(通过 Moq.Object.GetType(),其中 Moq 是存根实例),但我得到了System.MissingMethodException {"No parameterless constructor defined for this object."}
我究竟做错了什么?我不想更改我的主题代码以适应最小起订量。
测试
private Test() ...
{
var recordStub = new Mock<IRecord>();
var record = recordStub.Object;
var recordDefinition = new RecordDefinition()
{ MappedRecordClassType = record.GetType(), ...
var newRecord = CreateDataRecord(recordDefinition);
...
}
主题
private IRecord CreateDataRecord(RecordDefinition recordDefinition)
{
var result = Activator.CreateInstance(recordDefinition.MappedRecordClassType)
as IRecord;
return result;
}