What steps will reproduce the problem?
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Testing.Tests
{
[TestClass]
public class Tests
{
public interface ITest
{
Task<int> TestMethod(int? i);
}
[TestMethod]
public async Task MyTest()
{
var mock = new Mock<ITest>();
mock.Setup(m => m.TestMethod(It.IsAny<int>()))
.ReturnsAsync(0);
mock.Setup(m => m.TestMethod(It.IsAny<int?>()))
.ReturnsAsync(0);
await mock.Object.TestMethod(null);
}
}
}
What is the expected output? What do you see instead?
The test to pass without a Moq internal exception. A NullReferenceExecption
from Moq.
What version of the product are you using? On what operating system?
Moq - 4.2.1502.911
.NET - v4.0.20926
Windows 7 Professional SP 1 x64
Visual Studio Professional 2013 - 12.0.31101.00 Update 4
Please provide any additional information below.
This exception provides the following information:
Message: Object reference not set to an instance of an object.
StackTrace: at Moq.Match`1.Matches(Object value)
at Moq.Matchers.Matcher.Matches(Object value)
at Moq.MethodCall.Matches(ICallContext call)
at Moq.ExtractProxyCall.<>c__DisplayClass1.<HandleIntercept>b__0(IProxyCall c)
at System.Linq.Enumerable.LastOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
at Moq.ExtractProxyCall.HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx)
at Moq.Interceptor.Intercept(ICallContext invocation)
at Moq.Proxy.CastleProxyFactory.Interceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.ITestProxy.TestMethod(Nullable`1 i)
at Testing.Tests.<MyTest>d__0.MoveNext() in c:\Code\...\Tests.cs:line 31
The follow two code samples work as expected:
[TestMethod]
public async Task MyTest()
{
var mock = new Mock<ITest>();
//mock.Setup(m => m.TestMethod(It.IsAny<int>()))
// .ReturnsAsync(0);
mock.Setup(m => m.TestMethod(It.IsAny<int?>()))
.ReturnsAsync(0);
await mock.Object.TestMethod(null);
}
[TestMethod]
public async Task MyTest()
{
var mock = new Mock<ITest>();
mock.Setup(m => m.TestMethod(It.IsAny<int>()))
.ReturnsAsync(0);
mock.Setup(m => m.TestMethod(It.IsAny<int?>()))
.ReturnsAsync(0);
await mock.Object.TestMethod(0);
}
The issues seems to be a difficulty to evaluate what should be called due to
the `null` passed into the Moq'ed method, as the first setup is for type `int`
and the second is for type `int?`.
Original issue reported on code.google.com by mojoca...@gmail.com on 12 Mar 2015 at 5:04
Original issue reported on code.google.com by
mojoca...@gmail.com
on 12 Mar 2015 at 5:04