【发布时间】:2022-01-14 05:04:10
【问题描述】:
我在一个类似于下面的抽象类中有一个隐式运算符,它将数据转换为提供的类型。
public abstract class MyClass
{
private object dataHolder; // just for representation
// at implementation it tries to convert dataHolder object
// or returns null if failed
public abstract T? Convert<T>();
public static implicit operator byte[]?(MyClass obj) => obj.Convert<byte[]?>();
}
我正在尝试为此类创建单元测试
[TestMethod]
public void MyTestMethod()
{
Mock<MyClass> mockedClass = new() { CallBase = true };
mockedClass.Setup(x => x.Convert<byte[]?>()); // no return statement
// this should be null using implicit operator
byte[]? output = mockedClass.Object;
// however I am receiving an empty byte[] (length 0).
Assert.IsNull(output);
}
如何验证我的输出也可以为空?
【问题讨论】:
标签: c# unit-testing moq .net-6.0