2016-04-22 51 views
0

訂閱使用下面的代碼獲取IEnumerable集合數據時不支持異常。無法訂閱發佈的Collection對象。使用Moq測試事件聚合器訂閱的例外情況

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

例外:

「System.NotSupportedException」類型的異常出現在Moq.dll但在用戶代碼中沒有處理

其他信息:在非虛擬無效設置(在VB中可重寫)成員:m => m.GetEvent()。訂閱(It.IsAny())

+0

從讀取異常消息,是'ShowScreenEvent.Subscribe()'非虛擬? –

+0

'ShowScreenEvent'類正在從'PubSubEvent >'類 – venkat

+0

inherting看看這個問題http://stackoverflow.com/questions/35868184/nsubstitute-vs-prism-eventaggregator-assert-that-c​​alling-a -method-triggers-even/35889556#35889556(你肯定可以將這個從NSubstitute轉換爲Moq) – Haukinger

回答

0

您收到異常說Moq沒有設置方法因爲它是非虛擬的。 Moq不支持在沒有標記爲virtual的具體類型上使用模擬方法; see here欲瞭解更多信息。

在你的情況,你試圖模擬ShowScreenEvent.Subscribe(...),你說它是在它的基類PubSubEvent<IEnumerable<Customer>>上定義的。事實上,它是非虛擬的:

public SubscriptionToken Subscribe(Action<TPayload> action)