模拟对象调用原始动作

Mock object invoking original Action

我总是尽量避免使用服务定位器,但在这种情况下,我有从 Base 继承并引发事件(例如 INotifyPropertyChanged)的模型,我希望始终在 UI 线程上调度这些事件。 我不能使用 DI 容器来保持模型构造函数为空。

下面是我的代码。

    public abstract class Base
    {
        private static IMainThreadDispatcherService _dispatcherService;

    /// <summary>
    /// The main thread dispatcher.
    /// </summary>
    protected static IMainThreadDispatcherService DispatcherService
    {
        get
        {
            return _dispatcherService ??
                   (_dispatcherService = DependencyLocatorService.Resolve<IMainThreadDispatcherService>());
        }
    }

    /// <summary>
    /// Helper method to raise PropertyChanged events.
    /// </summary>
    /// <typeparam name="T">Type of the property.</typeparam>
    /// <param name="selectorExpression">The expression to pass the property name.</param>
    public virtual void OnPropertyChanged<T>(Expression<Func<T>> selectorExpression)
    {
        if (selectorExpression == null)
        {
            throw new ArgumentNullException("selectorExpression");
        }

        MemberExpression body = selectorExpression.Body as MemberExpression;
        if (body == null)
        {
            throw new ArgumentException("The body must be a member expression");
        }

        DispatcherService.RequestMainThreadAction(() =>
        {
            NotifyPropertyChanged(selectorExpression);
        });
    }
}

/// <summary>
/// A service that holds on the UI thread dispatcher.
/// </summary>
public interface IMainThreadDispatcherService
{
    /// <summary>
    /// Invoke an action on main thread.
    /// </summary>
    /// <param name="action">The Action to invoke.</param>
    /// <returns>True if successfully invoked.</returns>
    bool RequestMainThreadAction(Action action);
}

/// <summary>
/// This class is an abstraction of the service locator.
/// </summary>
public class DependencyLocatorService : IDependencyLocatorService
{
    /// <summary>
    /// Constructs a new object instance injecting all required dependencies.
    /// </summary>
    /// <typeparam name="T">Type of.</typeparam>
    /// <returns>The object instance constructed.</returns>
    public T IocConstruct<T>()
    {
        // A resolver
    }

    /// <summary>
    /// Resolves an instance of the specified type.
    /// </summary>
    /// <typeparam name="T">Type of.</typeparam>
    /// <returns>The object instance.</returns>
    public static T Resolve<T>() where T : class
    {
        try
        {
            // A resolver
        }
        catch(Exception)
        {}
        return null;
    }

    /// <summary>
    /// Registers a singleton instance of type T.
    /// </summary>
    /// <typeparam name="T">The type to register.</typeparam>
    /// <param name="instance">The instance.</param>
    public static void RegisterSingleton<T>(T instance) where T : class
    {
        try
        {
            // A resolver
        }
        catch (Exception)
        {
        }
    }
}

问题是当我想对继承自 Base 的模型进行单元测试时,事情开始变得困难。

我正在寻求更改架构以启用适当的单元测试并模拟方法 DispatcherService.RequestMainThreadAction,但仍会引发事件。 我正在使用 Moq 框架,但不确定是否可以设置这种模拟,因为我希望调用原始 Action。

除非您确实需要 Mock,否则为什么不使用简单的存根呢?

public class DispatcherServiceStub : IMainThreadDispatcherService 
{
  public bool RequestMainThreadAction(Action action)
  {
    action();
  }
}

您可以尝试在 Moq 设置中使用回调方法并调用其中的操作吗?