0

Getting Not Supported exception while subscribing to fetch IEnumerable collection data with the below code. Not able to subscribe the published Collection object.

Mock<IEventAggregator> _mockEventAgg = new Mock<IEventAggregator>();
_mockEventAgg.Setup(x => x.GetEvent<ShowScreenEvent>().Publish(new ObservableCollection<Customer>()
              { 
                 // Customer properties or details     
              }));


_mockEventAgg.Setup(m => m.GetEvent<ShowScreenEvent>().Subscribe(It.IsAny<Action<IEnumerable<Customer>>>()))
             .Callback<IEnumerable<Customer>>(customers => SelectedCustomerData = customers);

Exception:

An exception of type 'System.NotSupportedException' occurred in Moq.dll but was not handled in user code

Additional information: Invalid setup on a non-virtual (overridable in VB) member: m => m.GetEvent().Subscribe(It.IsAny())

venkat
  • 5,648
  • 16
  • 58
  • 83
  • From reading the exception message, is `ShowScreenEvent.Subscribe()` non-virtual? – Patrick Quirk Apr 22 '16 at 17:31
  • `ShowScreenEvent` class is inherting from `PubSubEvent>` class – venkat Apr 22 '16 at 17:36
  • Have a look at this question http://stackoverflow.com/questions/35868184/nsubstitute-vs-prism-eventaggregator-assert-that-calling-a-method-triggers-even/35889556#35889556 (you can for sure transform this from NSubstitute to Moq) – Haukinger Apr 23 '16 at 11:44

2 Answers2

1

You would need to add a parameter to the setup so that you are mocking the correct subscribe method. in prism only one of the subscribe methods for an eent with a payload is marked as virtual

it would look something like this

Eventaggregator.Setup(c => c.GetEvent<YourEvent>()
                             .Subscribe(It.IsAny<Action<string>>(),
                                        It.IsAny<ThreadOption>(), 
                                        It.IsAny<bool>(),
                                        It.IsAny<Predicate<string>>()))
                .Returns(YourHandleFunc);
adiga
  • 34,372
  • 9
  • 61
  • 83
Kolpime
  • 51
  • 3
0

The exception you're getting says Moq is failing to set up a method because it is non-virtual. Moq does not support mocking methods on a concrete type that are not marked virtual; see here for more information.

In your case, you're attempting to mock ShowScreenEvent.Subscribe(...), which you say is defined on its base class, PubSubEvent<IEnumerable<Customer>>. Indeed, it is non-virtual:

public SubscriptionToken Subscribe(Action<TPayload> action)
Community
  • 1
  • 1
Patrick Quirk
  • 23,334
  • 2
  • 57
  • 88