模拟抽象类的默认行为

模拟抽象类的默认行为

本文介绍了使用 Rhino 模拟抽象类的默认行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对嘲笑很陌生,所以这可能是我还没有接受的东西,但我在任何地方都找不到好的例子.

I'm pretty new to mocking so this might be something I'm just not picking up on yet, but I can't find a good example anywhere.

我试图断言默认情况下,从我的抽象类继承的任何类都将在构造函数中实例化一个集合.这是抽象类:

I'm trying to assert that by default, any class that inherits from my abstract class will instantiate a collection in the constructor. Here's the abstract class:

public abstract class DataCollectionWorkflow : SequentialWorkflowActivity
{
        private readonly DataSet _output = new DataSet();
        private List<DataCollectionParameter> _params = null;

        public DataCollectionWorkflow()
        {
            _params = new List<DataCollectionParameter>();
        }

        public virtual IList<DataCollectionParameter> Parameters
        {
            get { return _params; }
            set { _params = (List<DataCollectionParameter>)value; }
        }
}

我如何用 Rhino 模拟这个?如果我执行 GenerateMock(或存根),构造函数将运行并且模拟的私有字段_params"被初始化,但模拟的Parameters" 属性为空.

How do I mock this with Rhino? If I do a GenerateMock<DataCollectionWorkflow> (or a stub), the constructor runs and the mock's private field "_params" gets initialized, but the mock's "Parameters" property is simply null.

显然生成的模拟子类覆盖了属性实现.是否有某种方法可以避免重新实现 Parameters 属性?

Obviously the generated mock subclass is overriding the property implementation. Is there some way of excluding the Parameters property from being re-implemented?

谢谢.

推荐答案

好的,我想通了.把我归为 Rhino 错综复杂的另一个受害者.这种事情让我想转向更简单的框架,也许我会查看 MoQ.

Okay, I figured it out. Chalk me up as another casualty to the intricacies of Rhino. This kind of thing makes me want to move to a simpler framework, maybe I'll check out MoQ.

所以答案是使用 PartialMocks.我曾短暂尝试过生成一个部分模拟,但是当我运行调试器时,我注意到这些属性甚至不是空的,它们抛出了奇怪的异常,所以我没有深入研究.我使用的是简短的 AAA 类型的语法.

So the answer is using PartialMocks. I had briefly toyed with generating a partial mock, but when I ran the debugger over it I noticed that the properties weren't even null, they were throwing weird exceptions, so I didn't look much deeper. I was using the short form AAA type of syntax.

事实证明,如果我只是将模拟置于播放模式,则测试有效 - 属性按原样使用(因为它们应该与部分模拟一起使用).

Turns out if I simply put the mock into playback mode, the test works - the properties are used as is (as they should be with a partial mock).

答案如下:

[Test]
public void ShouldCreateParameterListInConstructor()
{
      var mockRepository = new MockRepository();
      var mock = mockRepository.PartialMock<DataCollectionWorkflow>();
      using ( mockRepository.Record() )
      {

      }
      using (mockRepository.Playback())
      {
           Assert.That(mock.Parameters, Is.Not.Null, "DataCollectionWorkflow base class didn't create new param collection");
      }
}

我意识到这是一个有状态测试,但它实际上是一些涉及相关属性的行为测试的简单前奏,因此我希望将此案例作为先决条件.希望它可以帮助某人.

I realize that this is a stateful test but it's actually a simpler prelude to some behavioural testing that involves the property in question, so I wanted this case as prerequisite. Hope it helps someone.

这篇关于使用 Rhino 模拟抽象类的默认行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:43