当有 "this" 参数时 AssertWasCalled

AssertWasCalled when there is a "this" parameter

我正在尝试测试 Caliburn.Micro 方法,PublishOnUIThread() 被调用:

public static class EventAggregatorExtensions
{
 ...        
      public static void PublishOnUIThread(this IEventAggregator eventAggregator, object message);
 ...
}

我正在尝试这样测试:

//assert
_eventAggregator.AssertWasCalled(x => 
x.PublishOnUIThread(Arg<object>
.Is.Anything));

但出现错误:

System.InvalidOperationException : When using Arg<T>, all arguments must be defined using Arg<T>.Is, Arg<T>.Text, Arg<T>.List, Arg<T>.Ref or Arg<T>.Out. 2 arguments expected, 1 have been defined.

但是,我无法强制该方法采用 2 个参数。我是测试新手,所以我不确定如何解决这个问题。

根据框架文档,The Event Aggregator定义如下。

public interface IEventAggregator {
    bool HandlerExistsFor(Type messageType);
    void Subscribe(object subscriber);
    void Unsubscribe(object subscriber);
    void Publish(object message, Action<Action> marshal);
}

如您所知,PublishOnUIThread是一个Event Aggregator Extension方法,应用于调用void Publish(object message, Action<Action> marshal);方法的接口。

在这种情况下,您可以在尝试测试发布事件时断言该方法 (IEventAggregator.Publish),因为您无法模拟扩展方法。

//assert
_eventAggregator.AssertWasCalled(x => 
x.Publish(Arg<object>.Is.Anything, Arg<Action<Action>>.Is.Anything));