在嘲笑抽象类筹款活动失败

在嘲笑抽象类筹款活动失败

本文介绍了RhinoMocks的 - 在嘲笑抽象类筹款活动失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道我可以在一个抽象类引发事件?



测试失败,下面就上线。我得到例外的是以下内容:



I am able to raise the event on an interface, but not on an abstract class that implements that interface. This is using the latest build of RhinoMocks (3.6.0.0).

Thanks,Alex

    public abstract class SomeClass : SomeInterface
    {
        public event EventHandler SomeEvent;
    }

    public interface SomeInterface
    {
        event EventHandler SomeEvent;
    }

    [Test]
    public void Test_raising_event()
    {
        var someClass = MockRepository.GenerateMock<SomeClass>();
        var someInterface = MockRepository.GenerateMock<SomeInterface>();

        someInterface.Raise(x => x.SomeEvent += null, someClass, EventArgs.Empty);
        someClass.Raise(x => x.SomeEvent += null, someClass, EventArgs.Empty);
    }
解决方案

Problem is explained by exception message:

Your event is not virtual, ie. Rhino won't be able to override it. Simply add virtual keyword to your abstract class event definition.

Bit background information. When you call MocksRepository.GenerateMock<SomeClass> Rhino will create dynamic proxy class, which it will use to record calls, prepare stubs and so forth. This class may look +/- like this:

public class SomeClassDynamicProxy1 : SomeClass
{
    public override EventHandler SomeEvent
    {
        add { ... }
        remove { ... }
    }

    ...
}

Without virtual in your SomeClass, this code will naturaly fail as it does now.

这篇关于RhinoMocks的 - 在嘲笑抽象类筹款活动失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:43