在我的单元测试中,如何验证模拟对象是否引发了事件。
我有一个视图(UI)--> ViewModel--> DataProvider--> ServiceProxy。ServiceProxy 对服务操作进行异步调用。当异步操作完成时,调用 DataProvider 上的方法(回调方法作为方法参数传递)。然后回调方法引发 ViewModel 正在侦听的事件。
对于 ViewModel 测试,我模拟 DataProvider 并验证 DataProvider 引发的事件的处理程序是否存在。在测试 DataProvider 时,我模拟了 ServiceProxy,但是如何测试调用回调方法并引发事件。
我正在使用 RhinoMock 3.5 和 AAA 语法
谢谢
-- 数据提供者 --
public partial class DataProvider
{
public event EventHandler<EntityEventArgs<ProductDefinition>> GetProductDefinitionCompleted;
public void GetProductDefinition()
{
var service = IoC.Resolve<IServiceProxy>();
service.GetProductDefinitionAsync(GetProductDefinitionAsyncCallback);
}
private void GetProductDefinitionAsyncCallback(ProductDefinition productDefinition, ServiceError error)
{
OnGetProductDefinitionCompleted(this, new EntityEventArgs<ProductDefinition>(productDefinition, error));
}
protected void OnGetProductDefinitionCompleted(object sender, EntityEventArgs<ProductDefinition> e)
{
if (GetProductDefinitionCompleted != null)
GetProductDefinitionCompleted(sender, e);
}
}
-- 服务代理 --
public class ServiceProxy : ClientBase<IService>, IServiceProxy
{
public void GetProductDefinitionAsync(Action<ProductDefinition, ServiceError> callback)
{
Channel.BeginGetProductDefinition(EndGetProductDefinition, callback);
}
private void EndGetProductDefinition(IAsyncResult result)
{
Action<ProductDefinition, ServiceError> callback =
result.AsyncState as Action<ProductDefinition, ServiceError>;
ServiceError error;
ProductDefinition results = Channel.EndGetProductDefinition(out error, result);
if (callback != null)
callback(results, error);
}
}