我有一些简单的界面,我正在编写一个简单的单元测试来调用Mock<>
。
public interface IMy() {
void Method(ISomething foo, byte[] bar);
}
var mock = new Mock<IMy>;
// ...
mock.Verify(m => m.Method(It.IsAny<ISomething>(), It.IsAny<byte[]>()), Times.Exactly(10));
但是,Verify
会引发以下异常:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Linq.Enumerable.<OfTypeIterator>d__92`1.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.String.Join(String separator, IEnumerable`1 values)
at Moq.Extensions.GetValue(Object value)
at Moq.Extensions.<>c.<Format>b__1_1(Object a)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Moq.Extensions.Format(ICallContext invocation)
at Moq.Mock.<>c.<FormatInvocations>b__63_0(ICallContext i)
at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Moq.Mock.FormatInvocations(IEnumerable`1 invocations)
at Moq.Mock.ThrowVerifyException(MethodCall expected, IEnumerable`1 setups, IEnumerable`1 actualCalls, Expression expression, Times times, Int32 callCount)
at Moq.Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times)
at Moq.Mock.Verify[T,TResult](Mock`1 mock, Expression`1 expression, Times times, String failMessage)
at Moq.Mock`1.Verify[TResult](Expression`1 expression, Times times)
调试器在这里找不到null
的确切内容并没什么帮助。有没有人使用Moq
库遇到它?
答案 0 :(得分:1)
最终问题在于Moq内部使用模拟的IEnumerable
实现。在库的未来版本中,错误消息和前置条件检查得到了改进。
请参阅以下讨论以供参考:
第一个错误 - 堆栈跟踪不同但根本原因: https://github.com/moq/moq4/issues/169
第二个错误 - 相同的堆栈跟踪: https://github.com/moq/moq4/issues/464
更好地检查Github库是否存在开源故障。