中发布事件
我正在为我的ViewModels编写UNITSEST。在我的ViewModel中,我有:
private IEventAggregator _eventAggregator;
public MyViewModel( IEventAggregator eventAggregator ) {
_eventAggregator = eventAggregator;
_eventAggregator.GetEvent<ResponseParameters>( ).Subscribe( OnResponseParametersSave );
}
private void OnResponseParametersSave (ResponseParameters item ){
var x = item
}
现在在我的Untest中,我要做以下操作:
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如何在Untest
猜猜您正在使用Prism(ieventaggregator和Unity)。
我几年前使用棱镜,不得不写
_eventAggregator.GetEvent<ResponseParameters>().Publish( new ResponseParameters ( ) { item = _item } );
开火和事件。但是我想这只是一个较旧的语法。
您是否尝试过没有统一的ViewModel的实例?
_eventAggregator = new EventAggregator();
_vm = new MyViewModel>(_eventAggreator);
只是为了统一并没有遇到任何麻烦。