犀牛制品模拟部分 [英] Rhino Mocks Partial Mock

查看:271
本文介绍了犀牛制品模拟部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一些现有的类测试的逻辑。它是目前不可能重新因素的类,因为它们是非常复杂的,在生产中。



我想要做的就是创建一个模拟对象和测试方法是什么在内部调用另一种方法,是非常困难的嘲笑。



所以我想刚才设置为辅助方法调用的行为。



但是,当我设置该方法的行为,该方法的代码被调用失败。



我缺少的东西,或者这是不可能的测试,而无需重新融通类?



我已经尝试了所有不同的模拟类型(陈吉伟,存根,动态,部分等。),但他们都最终调用方法时我尝试设置的行为。

 使用系统; 
使用MbUnit.Framework;使用Rhino.Mocks
;

命名空间MMBusinessObjects.Tests
{
[的TestFixture]
公共类PartialMockExampleFixture
{
[测试]
公共无效Simple_Partial_Mock_Test ()
{
常量字符串参数=任何东西;

//设置嘲笑
MockRepository嘲笑=新MockRepository();


VAR mockTestClass = mocks.StrictMock<&的TestClass GT;();

//记录beahviour *** actualy调用到真正的方法存根***
Expect.Call(mockTestClass.MethodToMock(参数))返回(真)。

//永远到不了这里
mocks.ReplayAll();

//这是我想测试
Assert.IsTrue(mockTestClass.MethodIWantToTest(参数));


}

公共类识别TestClass
{
公共BOOL MethodToMock(字符串参数)
{
/ /一些逻辑,这是非常难以模拟
抛出新NotImplementedException();
}

公共BOOL MethodIWantToTest(字符串参数)
{
//此方法调用
如果(MethodToMock(参数))
{
//一些逻辑,我想测试
}

返回真;
}
}
}
}


解决方案

MethodToMock不是虚拟的,因此不能被嘲笑。你想要做什么是可能与部分模拟(我在你类似的情况下做到了),但你要模拟出必须是一个接口实现的一部分或者被标记的虚拟方法。否则,你不能嘲笑Rhino.Mocks它。


I am trying to test the logic from some existing classes. It is not possible to re-factor the classes at present as they are very complex and in production.

What I want to do is create a mock object and test a method that internally calls another method that is very hard to mock.

So I want to just set a behaviour for the secondary method call.

But when I setup the behaviour for the method, the code of the method is invoked and fails.

Am I missing something or is this just not possible to test without re-factoring the class?

I have tried all the different mock types (Strick,Stub,Dynamic,Partial ect.) but they all end up calling the method when I try to set up the behaviour.

using System;
using MbUnit.Framework;
using Rhino.Mocks;

namespace MMBusinessObjects.Tests
{
    [TestFixture]
    public class PartialMockExampleFixture
    {
        [Test]
        public void Simple_Partial_Mock_Test()
        {
            const string param = "anything";

            //setup mocks
            MockRepository mocks = new MockRepository();


            var mockTestClass = mocks.StrictMock<TestClass>();

            //record beahviour *** actualy call into the real method stub ***
            Expect.Call(mockTestClass.MethodToMock(param)).Return(true);

            //never get to here
            mocks.ReplayAll();

            //this is what i want to test
            Assert.IsTrue(mockTestClass.MethodIWantToTest(param));


        }

        public class TestClass
        {
            public bool MethodToMock(string param)
            {
                //some logic that is very hard to mock
                throw new NotImplementedException();
            }

            public bool MethodIWantToTest(string param)
            {
                //this method calls the 
                if( MethodToMock(param) )
                {
                    //some logic i want to test
                }

                return true;
            }
        }
    }
}

解决方案

MethodToMock is not virtual and therefore can't be mocked. What you want to do is possible with a partial mock (I've done it in cases similar to yours), but the method you want to mock out must be either part of an interface implementation or be marked virtual. Otherwise, you can't mock it with Rhino.Mocks.

这篇关于犀牛制品模拟部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆