2

I am trying to test that Caliburn.Micro method, PublishOnUIThread() is called:

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

I am trying to test like this:

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

but get the error:

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.

However, I cannot force the method to take 2 parameters. I'm new to testing so I'm not sure how to work around this.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
azulBonnet
  • 831
  • 4
  • 14
  • 31
  • 2
    Not possible to mock extension methods in Rhino, http://stackoverflow.com/a/5488700/368552 You could wrap them in an interface however and mock the interface – Luke Hutton Feb 15 '17 at 19:33

1 Answers1

2

According to framework documentation, The Event Aggregator is defined as followed.

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

As you already know, PublishOnUIThread is an Event Aggregator Extension method applied to the interface which calls the void Publish(object message, Action<Action> marshal); method.

In this case you can assert that method (IEventAggregator.Publish) when trying to test publishing an event as you cannot mock the extension methods.

//assert
_eventAggregator.AssertWasCalled(x => 
x.Publish(Arg<object>.Is.Anything, Arg<Action<Action>>.Is.Anything));
Nkosi
  • 235,767
  • 35
  • 427
  • 472