我正在为ViewModels编写UnitTest。 在我的ViewModel中,我有:
private IEventAggregator _eventAggregator;
public MyViewModel( IEventAggregator eventAggregator ) {
_eventAggregator = eventAggregator;
_eventAggregator.GetEvent<ResponseParameters>( ).Subscribe( OnResponseParametersSave );
}
private void OnResponseParametersSave (ResponseParameters item ){
var x = item
}
现在在我的UnitTest中,我执行以下操作:
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" );
}
我在Method OnResponseParametersSave()上设置了断点,但在调试测试时没有触发。
有没有人知道我在做什么错误oe如何在UnitTest中发布事件
答案 0 :(得分:0)
猜猜你正在使用PRISM(IEventAggregator和Unity)。
几年前我使用PRISM并且不得不写
_eventAggregator.GetEvent<ResponseParameters>().Publish( new ResponseParameters ( ) { item = _item } );
开火和事件。但我想这只是一种较旧的语法。
您是否尝试过创建没有Unity的ViewModel实例?
_eventAggregator = new EventAggregator();
_vm = new MyViewModel>(_eventAggreator);
只是为了保佑Unity没有遇到任何麻烦。