C# 使用 fakeiteasy 在假对象上使用自定义委托类型引发事件

C# Raising Events with custom delegate type on fake object with fakeiteasy

根据 fakeiteasy 的文档,我所要做的就是:

public delegate void CustomEventHandler(object sender, CustomEventArgs e);

event CustomEventHandler CustomEvent;

fake.CustomEvent += Raise.With<CustomEventHandler>(fake, sampleCustomEventArgs);

我在我的代码中试过如下:

public delegate void RowStateHandler(object sender, RowStateHandlerArgs e);
public class RowStateHandlerArgs : EventArgs
{
    public bool Selected { get; set; }
    public string CampaignId { get; set; }
}

... 视图的界面:

public interface ICampaignChannelView
{
     event RowStateHandler RowStateChanged;
}

我的单元测试中的片段:

ICampaignChannelView v = A.Fake<ICampaignChannelView>();
RowStateHandlerArgs args = new RowStateHandlerArgs() {CampaignId = "1", Selected = true};
v.RowStateChanged += Raise.With<RowStateHandler>(v, args);

我得到以下编译错误:

Error   CS0029  Cannot implicitly convert type
FakeItEasy.Raise<Add_in.UI.Wizard.RowStateHandler> to
Add_in.UI.Wizard.RowStateHandler    Add-inTests C:\..\WizardPresenterTests.cs

Error CS1503 Argument 2: cannot convert from 'Add_in.UI.Wizard.RowStateHandlerArgs' to 'Add_in.UI.Wizard.RowStateHandler' Add-inTests C:..\WizardPresenterTests.cs

非常感谢任何帮助!

听起来您使用的 FakeItEasy 版本比该文档提到的要旧。 Raising Events documentation page has two flavours. One for FakeItEasy 1.x and one for 2.x.

(Update: the documentation has since been moved to readthedocs, which has a better system for maintaining different versions of the documentation.)

我刚刚查看了这两个页面,并使用您的代码构建了两个测试项目。我最终更改的唯一一行是

 v.RowStateChanged += Raise.With<RowStateHandler>(v, args);

在 FakeItEasy 1.25.3 下,此调用有效:

v.RowStateChanged += Raise.With(v, args).Now;

在 FakeItEasy 2.0.0 下,这个调用有效:

v.RowStateChanged += Raise.With<RowStateHandler>(v, args);