2

I have some simple interface and I'm writing a simple unit test that invokes a Mock<> of it.

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));

However, the Verify throws the following exception:

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)

Debugger doesn't help much to find what exactly could be null here. Did anyone encounter it using Moq library?

Mugen
  • 8,301
  • 10
  • 62
  • 140
  • 2
    Could you provide a [mcve] rather than just a snippet of pseudo-code? It would be easier to help you if we could reproduce the exact error. – Jon Skeet Apr 02 '18 at 11:08
  • 2
    Duplicate of "what is null"? Really... Ok I'll move the question to Moq github, maybe they'll care – Mugen Apr 02 '18 at 19:47
  • If you can demonstrate that it's *not* a duplicate, the question can be reopened. The best way of doing that is to provide a [mcve] as I suggested shortly after you asked the question. – Jon Skeet Apr 02 '18 at 19:49
  • 1
    Found the issue in their github, at least I learned to look there first. If you'll un-mark it as duplicate I could post an answer for anyone encountering this in the future. – Mugen Apr 03 '18 at 04:50
  • I can't do that - I don't have enough reputation. But even if I did, I probably wouldn't do it while the question is still in its current state - I'd want to see a [mcve] first. That would make the question *much* better. – Jon Skeet Apr 03 '18 at 05:23

1 Answers1

3

Ultimately the problem was that Moq internally uses the mock's implementation of IEnumerable. In future versions of the library, the error message and precondition checks were improved.

See following discussions for reference:

First error - not the same stack trace but root cause: https://github.com/moq/moq4/issues/169

Second error - same stack trace: https://github.com/moq/moq4/issues/464

Better check in a library Github when its an open source fault.

Mugen
  • 8,301
  • 10
  • 62
  • 140
  • Any workaround to solve this issue? I am still getting the same error (moq version is 4.14.2) – Sharun Jun 17 '20 at 09:41