venkat
venkat

Reputation: 5738

Exception on test of event aggregator subscriptions using Moq

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())

Upvotes: 0

Views: 1055

Answers (2)

Kolpime
Kolpime

Reputation: 51

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);

Upvotes: 1

Patrick Quirk
Patrick Quirk

Reputation: 23767

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)

Upvotes: 0

Related Questions