0
我正在爲ViewModels編寫UnitTest。 在我的ViewModel我有:UnitTest發佈事件不起作用
private IEventAggregator _eventAggregator;
public MyViewModel(IEventAggregator eventAggregator ) {
_eventAggregator = eventAggregator;
_eventAggregator.GetEvent<ResponseParameters>().Subscribe(OnResponseParametersSave);
}
private void OnResponseParametersSave (ResponseParameters item){
var x = item
}
現在在我的單元測試我做到以下幾點:
private IUnityContainer _unityContainer;
private IEventAggregator _eventAggregator;
[TestInitialize]
public async void TestInitialize() {
_unityContainer = new UnityContainer();
_unityContainer.RegisterType<IEventAggregator, EventAggregator>();
_eventAggregator = _unityContainer.Resolve<EventAggregator>();
_vm = _unityContainer.Resolve<MyViewModel>();
await _vm.Load();
}
[TestMethod]
public void AddingItemDataObjectsShouldThenBeLoaded() {
var _item = new MyObject{
Description = "ArticleOne",
Id = 2,
Comment = "Testing event and adding of item to list",
};
_eventAggregator.Publish(new ResponseParameters () { item = _item });
Assert.IsNotNull(_vm.Data, "The Data object is still null after adding an item");
}
我穿上了方法OnResponseParametersSave(斷點),但它不觸發,當我調試測試。
有沒有人有一個想法是什麼,我做錯了OE如何在單元測試
EventAggregator.Publish只是以難忘的方式發佈事件並返回。你應該在你的單元測試中提供你自己的IEventAggregator接口實現:http://stackoverflow.com/questions/3694195/moq-event-aggregator-is-it-possible。 – mm8