c# - 使用 Rhino 从模拟对象引发事件

标签 c# unit-testing events nunit rhino-mocks

我在模拟对象上引发事件时遇到问题。我正在使用Rhino Mocks 3.4。我研究过类似的问题,但未能重现任何建议的解决方案。

我有一个类 - Foo - 它有一个私有(private)方法,只能通过注入(inject)接口(interface) - IBar 的事件调用来访问。

如何从 RhinoMock 对象引发事件 IBar.BarEvent,以便我可以在 Foo 中测试该方法?

这是我的代码:

[TestFixture]
public sealed class TestEventRaisingFromRhinoMocks
{

    [Test]
    public void Test()
    {
        MockRepository mockRepository = new MockRepository();
        IBar bar = mockRepository.Stub<IBar>();

        mockRepository.ReplayAll();

        Foo foo = new Foo(bar);

        //What to do, if I want invoke bar.BarEvent with value =123??

        Assert.That(foo.BarValue, Is.EqualTo(123));

    }

}


public class Foo
{
    private readonly IBar _bar;
    private int _barValue; 

    public Foo(IBar bar)
    {
        _bar = bar;
        _bar.BarEvent += BarHandling; 
    }

    public int BarValue
    {
        get { return _barValue; }
    }

    private void BarHandling(object sender, BarEventArgs args)
    {
        //Eventhandling here: How do I get here with a Rhino Mock object? 
        _barValue = args.BarValue;
    }
}


public interface IBar
{
    event EventHandler<BarEventArgs> BarEvent;
}


public class BarEventArgs:EventArgs
{
    public BarEventArgs(int barValue)
    {
        BarValue = barValue;
    }
    public int BarValue { get; set; } 
}

最佳答案

我认为是这样的:

bar.Raise(x => x.BarEvent += null, this, EventArgs.Empty);

http://ayende.com/wiki/Rhino+Mocks+3.5.ashx#Howtoraiseevents

关于c# - 使用 Rhino 从模拟对象引发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6032955/

相关文章:

c# - 取消记住远程调试器凭据

c# - 使用最小起订量创建具有自动填充属性的模拟?

ruby-on-rails - Rails : Old, 不存在的数据库表导致测试出错

c# - 行动到事件

events - OnKeyDown事件不会立即更新,OnKeyUp是

c# - 使用忽略重载的 lambda 语法选择方法表达式

c# - WinForms 到 WPF 的迁移

java - void 方法的单元测试应该是什么样子?

php - Laravel 5.2 单元测试集标题不起作用

php - 如何从 Observer 获取 Magento Order 数据