Rhino Mocks Partial Mock

I am trying to check the logic from some existing classes. It is currently not possible to regroup classes because they are very complex and produced.

What I want to do is create a mock object and test a method that internally calls another method that is very difficult to make fun of.

So, I want to just set the behavior to call the secondary method.

But when I tweak the behavior of the method, the method code gets called and fails.

Am I missing something or is it just impossible to verify without re-factoring the class?

I tried all different layouts (Strick, Stub, Dynamic, Partial ect.), But all of them end up calling the method when I try to customize the behavior.

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;
            }
        }
    }
}
+5
2

MethodToMock . , , ( , ), , , , . Rhino.Mocks.

+14

, , , . , .

 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();
    }
+1

All Articles