gpt4 book ai didi

c# - MVP、WinForms、单元测试、最小起订量、NUnit : Verify that event handlers have been added to a dependency

转载 作者:行者123 更新时间:2023-11-30 23:15:08 26 4
gpt4 key购买 nike

我有演示者、 View 和模型。 View 不知道演示者。Presenter 协调一切并仅通过界面了解 View :

public interface ISuspiciousListManagementView
{

void ShowMessageBox(MessageBoxButtons messageBoxButtons, string message, string caption, EMessageBoxIcons icon);

void ShowDialog(IWin32Window owner);
object AllTypesDataSource { get; set; }
SuspiciousListType SelectedType { get; set; }
event EventHandler SelectedTypeChanged;
string FilePath { get; set; }

bool ShowOnlyActive { get; set; }
object AllHistoryAvailableTypesDataSource { get; set; }
SuspiciousFilteredListType SelectedHistoryType { get; set; }
event EventHandler SelectedHistoryTypeChanged;

object HistoryDataGridSource { get; set; }

event EventHandler SelectedShowOnlyActiveChanged;

ICommand BrowseFileCommand { get; set; }
ICommand UploadCommand { get; set; }
ICommand RefreshCommand { get; set; }

}

Presenter 持有方法 Init:

 public async Task Init()
{
_view.AllTypesDataSource = Enum.GetValues(typeof(SuspiciousListType));
_view.AllHistoryAvailableTypesDataSource = Enum.GetValues(typeof(SuspiciousFilteredListType));
_view.SelectedTypeChanged += OnSelectedTypeChanged;
_view.SelectedHistoryTypeChanged += OnSelectedHistoryTypeChanged;
_view.SelectedShowOnlyActiveChanged += OnSelectedShowOnlyActiveChanged;
_view.HistoryDataGridSource = await GetFilterHistoryItems();
}

和文件名:

  private readonly ISuspiciousListManagementView _view;

这是通过构造函数注入(inject)的。

我需要验证这些代码行是否已经运行:

    _view.SelectedTypeChanged += OnSelectedTypeChanged;
_view.SelectedHistoryTypeChanged += OnSelectedHistoryTypeChanged;
_view.SelectedShowOnlyActiveChanged += OnSelectedShowOnlyActiveChanged;

我想运行这样的东西:

 [Test]
public async void Presenter_Verify_HistoryDataGridSource_assigned_Test()
{
await _presenter.Init();
_viewMock.Verify(dp => dp.SelectedHistoryTypeChanged != null);//Compilation error
}

如何测试是否已添加事件处理程序?我的设计有什么问题?

在此先感谢您的帮助。

最佳答案

在“自动”事件的情况下,像这样:

class Test {
public event Action MyEvent;
}

可以通过反射获取调用列表:

var eventField = test.GetType().GetField(nameof(test.MyEvent), BindingFlags.Instance | BindingFlags.NonPublic);
var eventDelegate = eventField.GetValue(test);
//either get invocation list or just compare eventDelegate with null
var invocationList = eventDelegate != null ? ((Delegate)eventDelegate).GetInvocationList() : new Delegate[0];
bool anySubscribers = invocationList.Length > 0;

那是因为编译器将 MyEvent 扩展为(非常粗略地)如下:

class Test {
private Action MyEvent;

public event Action MyEvent
{
add { Delegate.Combine(this.MyEvent, (Delegate) value);}
remove { Delegate.Remove(this.MyEvent, value); }
}
}

我们可以获得该私有(private)(编译器生成的)字段的值。

如果事件被显式实现:

class Test {        
public event Action MyEvent
{
add
{
// do something

}
remove
{
// do something else
}
}
}

你不能这样做,因为甚至可能不是订阅者的概念 - addremove 可以执行任意操作。但这很可能不是你的情况。

关于c# - MVP、WinForms、单元测试、最小起订量、NUnit : Verify that event handlers have been added to a dependency,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42763668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com