我正在使用MVP Passive View模式创建WinForms应用程序。我的视图会触发事件,然后由各自的演示者处理。我试图确保在演示者中使用RhinoMocks和Moq处理事件无济于事。我找不到强制RhinoMocks忽略我的Presenter事件处理程序行为的方法,也找不到让Moq实际调用事件处理程序的方法。
任何建议都将不胜感激。作为参考,这是我的代码的简化版本。我希望能够验证,在FileCellClicked被触发时,FileTabPresenter中的FileCellClickedAsync()被调用。
〜编辑〜我还包括了我想要测试的基本概念。不幸的是,我从Moq得到一个Exception,说永远不会执行调用。
主讲人:
public class FilesTabPresenter {
private readonly IFilesTabView mainView;
public FilesTabPresenter(IFilesTabView inView) {
mainView = inView;
mainView.FileCellClicked += FileCellClickedAsync;
}
public virtual async void FileCellClickedAsync(object sender, GridViewCellEventArgs e) {
//...logic...
}
查看:
public partial class FilesTab : UserControl, IFilesTabView {
public event GridViewCellEventHandler FileCellClicked;
public FilesTab() {
InitializeComponent();
}
private void radGridViewFiles_CellClick(object sender, GridViewCellEventArgs e) {
OnFileCellClicked(sender, e);
}
private void OnFileCellClicked(object sender, GridViewCellEventArgs args) {
var eventHandler = this.FileCellClicked;
if (eventHandler != null) {
eventHandler.Invoke(sender, args);
}
}
查看界面:
public interface IFilesTabView {
event GridViewCellEventHandler FileCellClicked;
}
(非功能性)测试代码:
public void VerifyFileCellClickedEventHandled() {
var viewMock = new Mock<IFilesTabView>();
var presenterStub = new Mock<FilesTabPresenter>(viewMock.Object);
viewMock.Raise(v => v.FileCellClicked += null, It.IsAny<EventArgs>(), new GridViewCellEventArgs(null, null, null));
presenterStub.Verify(x => x.FileCellClickedAsync(It.IsAny<EventArgs>(), It.IsAny<GridViewCellEventArgs>()), Times.AtLeastOnce());
}