Rhino Mocks 部分模拟

发布于 2024-08-31 02:50:44 字数 1602 浏览 3 评论 0原文

我正在尝试测试一些现有类的逻辑。目前不可能重构这些类,因为它们非常复杂并且正在生产中。

我想做的是创建一个模拟对象并测试一个在内部调用另一个很难模拟的方法的方法。

所以我只想为辅助方法调用设置一个行为。

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

我是否遗漏了一些东西,或者如果不重构课程就无法进行测试?

我已经尝试了所有不同的模拟类型(Strick、Stub、Dynamic、Partial 等),但当我尝试设置行为时,它们最终都会调用该方法。

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;
            }
        }
    }
}

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;
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笔芯 2024-09-07 02:50:45

MethodToMock 不是虚拟的,因此不能被模拟。您想要做的事情可以通过部分模拟来实现(我已经在与您类似的情况下完成了),但是您想要模拟的方法必须是接口实现的一部分或被标记为虚拟。否则,您无法使用Rhino.Mocks 来模拟它。

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.

困倦 2024-09-07 02:50:45

我建议不要在被测类中模拟方法,但您的情况可能很独特,因为您目前无法重构该类以使其更容易测试。您可以尝试显式创建委托以防止在设置调用时调用该方法。

 Expect.Call( delegate { mockTestClass.MethodToMock(param) } ).Return(true);

或者,切换到使用 AAA 语法,省略已弃用的结构。

    [Test]
    public void Simple_Partial_Mock_Test()
    {
        const string param = "anything";

        var mockTestClass = MockRepository.GenerateMock<TestClass>();

        mockTestClass.Expect( m => m.MethodToMock(param) ).Return( true );

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

        mockTestClass.VerifyAllExpectations();
    }

I recommend not mocking methods in the class under test, but your situation may be unique in that you can't refactor the class to make it easier to test at present. You might try explicitly making a delegate to prevent the method from being invoked when setting up the call.

 Expect.Call( delegate { mockTestClass.MethodToMock(param) } ).Return(true);

Or, switch to using the AAA syntax, omitting the deprecated constructs.

    [Test]
    public void Simple_Partial_Mock_Test()
    {
        const string param = "anything";

        var mockTestClass = MockRepository.GenerateMock<TestClass>();

        mockTestClass.Expect( m => m.MethodToMock(param) ).Return( true );

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

        mockTestClass.VerifyAllExpectations();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文