我总是尽量避免使用Service Locator,但是在这种情况下,我有从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。
答案 0 :(得分:1)
除非您确实需要Mock
,否则为什么不使用简单的存根?
public class DispatcherServiceStub : IMainThreadDispatcherService
{
public bool RequestMainThreadAction(Action action)
{
action();
}
}
答案 1 :(得分:1)
您可以尝试在Moq设置中使用Callback方法并调用其中的操作吗?