使变量成为引用类型

Make a variable a reference type

我的 C# 测试中有以下模拟:

private Mock<Offset> _mockOffsetPosition = new Mock<Offset>(50);

Offset 是一个 Confluent.Kafka.Offset 结构。

它给我这个错误:

CS0452: The type 'Offset' must be a reference type in order to use it as a parameter 'T' in the generic type or method 'Mock'

我该如何解决?

你不要嘲笑它。让我们看看 what mocking is:

In a unit test, mock objects can simulate the behavior of complex, real objects and are therefore useful when a real object is impractical or impossible to incorporate

因此,不要模拟,只需使用您需要的值进行初始化并传递真正的结构。所有值类型都应如此。

private Offset _mockOffsetPosition = new Offset(50);