异步方法返回null

时间:2013-05-09 00:04:17

标签: c# mocking moq async-await

如果我尝试模拟包含async方法的类型,例如:

interface Foo
{
    Task<int> Bar();
}

然后mock的Bar方法返回null。我想Moq选择default(Task<int>)作为我方法的默认返回值,实际上是null。但是,Moq应该选择Task.FromResult(default(int))之类的默认值。我可以强制Moq使异步方法返回非空任务吗?

4 个答案:

答案 0 :(得分:33)

如果有人感兴趣,我制作了一个扩展类,它使异步方法更简洁:

public static class SetupExtensions
{
    public static IReturnsResult<TMock> ReturnsTask<TMock, TResult>(
        this ISetup<TMock, Task<TResult>> setup) where TMock : class
    {
        return setup.Returns(() => Task.FromResult(default(TResult)));
    }

    public static IReturnsResult<TMock> ReturnsTask<TMock, TResult>(
        this ISetup<TMock, Task<TResult>> setup, TResult value) where TMock : class
    {
        return setup.Returns(() => Task.FromResult(value));
    }

    public static IReturnsResult<TMock> ReturnsTask<TMock, TResult>(
        this ISetup<TMock, Task<TResult>> setup, Func<TResult> func) where TMock : class
    {
        return setup.Returns(Task.Factory.StartNew(func));
    }

    public static IReturnsResult<TMock> ReturnsTask<TMock, T, TResult>(
        this ISetup<TMock, Task<TResult>> setup, Func<T, TResult> func) where TMock : class
    {
        return setup.Returns<T>(arg => Task.Factory.StartNew(() => func(arg)));
    }

    public static IReturnsResult<TMock> ReturnsTask<TMock, T1, T2, TResult>(
        this ISetup<TMock, Task<TResult>> setup, Func<T1, T2, TResult> func) where TMock : class
    {
        return setup.Returns<T1, T2>((arg1, arg2) => Task.Factory.StartNew(() => func(arg1, arg2)));
    }

    public static IReturnsResult<TMock> ReturnsTask<TMock, T1, T2, T3, TResult>(
        this ISetup<TMock, Task<TResult>> setup, Func<T1, T2, T3, TResult> func) where TMock : class
    {
        return setup.Returns<T1, T2, T3>((arg1, arg2, arg3) => Task.Factory.StartNew(() => func(arg1, arg2, arg3)));
    }

    public static IReturnsResult<TMock> ReturnsTask<TMock, T1, T2, T3, T4, TResult>(
        this ISetup<TMock, Task<TResult>> setup, Func<T1, T2, T3, T4, TResult> func) where TMock : class
    {
        return setup.Returns<T1, T2, T3, T4>((arg1, arg2, arg3, arg4) => Task.Factory.StartNew(() => func(arg1, arg2, arg3, arg4)));
    }

    public static IReturnsResult<TMock> ReturnsTask<TMock>(this ISetup<TMock, Task> setup, Action action) where TMock : class
    {            
        return setup.Returns(Task.Factory.StartNew(action));
    }

    public static IReturnsResult<TMock> ReturnsTask<TMock, T>(this ISetup<TMock, Task> setup, Action<T> action) where TMock : class
    {            
        return setup.Returns<T>(arg => Task.Factory.StartNew(() => action(arg)));
    }

    public static IReturnsResult<TMock> ReturnsTask<TMock, T1, T2>(this ISetup<TMock, Task> setup, Action<T1, T2> action) where TMock : class
    {            
        return setup.Returns<T1, T2>((arg1, arg2) => Task.Factory.StartNew(() => action(arg1, arg2)));
    }

    public static IReturnsResult<TMock> ReturnsTask<TMock, T1, T2, T3>(this ISetup<TMock, Task> setup, Action<T1, T2, T3> action) where TMock : class
    {            
        return setup.Returns<T1, T2, T3>((arg1, arg2, arg3) => Task.Factory.StartNew(() => action(arg1, arg2, arg3)));
    }

    public static IReturnsResult<TMock> ReturnsTask<TMock, T1, T2, T3, T4>(this ISetup<TMock, Task> setup, Action<T1, T2, T3, T4> action) where TMock : class
    {            
        return setup.Returns<T1, T2, T3, T4>((arg1, arg2, arg3, arg4) => Task.Factory.StartNew(() => action(arg1, arg2, arg3, arg4)));
    }

    public static IReturnsResult<TMock> ReturnsTask<TMock>(this ISetup<TMock, Task> setup) where TMock : class
    {
        return setup.Returns(Task.Factory.StartNew(delegate {  }));
    }
}

一些例子:

//Example 1 :
public interface IFoo
{
    Task Bar();
}

var mock = new Mock<IFoo>();

mock.Setup(m => m.Bar()).ReturnsTask(); //await Bar() will return void

//Example 2 :
public interface IFoo
{
    Task<int> Bar();
}

var mock = new Mock<IFoo>();

mock.Setup(m => m.Bar()).ReturnsTask(); //await Bar() will return default(int)

//Example 3 :
public interface IFoo
{
    Task<int> Bar();
}

var mock = new Mock<IFoo>();

mock.Setup(m => m.Bar()).ReturnsTask(4); //await Bar() will return 4;

//Example 4 :
public interface IFoo
{
    Task<int> Bar(int x, int y);
}

var mock = new Mock<IFoo>();

mock.Setup(m => m.Bar(It.IsAny<int>(), It.IsAny<int>()))
                     .ReturnsTask<IFoo, int, int, int>((x,y) => x + y); //await Bar(x, y) will return x + y;

答案 1 :(得分:6)

您只需要存根Bar方法,然后返回Task.FromResult(default(int))

答案 2 :(得分:3)

在Moq 4.2中看起来这个问题是fixed。所以你只需要升级到最新版本的Moq(至少它开始在我的情况下返回非空任务)

答案 3 :(得分:2)

回想一下,Moq框架是开源的。在代码库(可用here)中,我们可以看到,当执行尚未设置的方法调用时,返回值是{{1}中valueDel私有字段的结果} .class。该字段被实例化,以便返回结果类型的默认值:

MethodCallReturn<TMock, TResult>

您可以添加一个方法,该方法将覆盖从Mock返回的给定类型的默认值,或者在 private Delegate valueDel = (Func<TResult>)(() => default(TResult)); 情况下显式返回建议的默认值。

您还可以在Moq issues list page上提交问题。

尽管没有处理Moq源代码,但您必须只是将Task接口存根,就像aquaraga建议的那样。可以找到模拟和存根之间差异的快速解释here