vendredi 4 mars 2016

Impossible to run test with TestCaseData generated from RhinoMocks stub

I am working with C# in Visual Studio 2015 Community, with NUnit3 and Rhino Mocks and attempting to write a test for a component of my system (it is NOT a unit test).

I have encountered a problem when trying to use a generated stub as the argument to a TestCaseData that is provided to a TestCaseSource. I get the following errors in the output window:

Test adapter sent back a result for an unknown test case. Ignoring result for 'MyTest(Castle.Proxies.IMyInterfaceProxy4c6c716794ef48818f41fd5012345ead)'.
Test adapter sent back a result for an unknown test case. Ignoring result for 'MyTest(Castle.Proxies.IMyInterfaceProxy4c6c716794ef48818f41fd5012345ead)'.

The test name appears in the VS2015 integrated test-runner when I rebuild the test project, but as soon I try to run it it becomes greyed out.

Here there is some sample code based on my test code:

using NUnit.Framework;
using Rhino.Mocks;
using System.Collections;

namespace FenixLib.Core.Tests
{
    public interface IMyInterface
    {
        int Property { get; }
    }

    [TestFixture]
    class TestMocks
    {
        [Test, TestCaseSource( "TestCases" )]
        public void MyTest(IMyInterface what)
        {
            // Do stuff
        }

        public static IEnumerable TestCases()
        {
            yield return new TestCaseData ( CreateFake ( 2 ) );
            yield return new TestCaseData ( CreateFake ( 4 ) );
        }

        public static IMyInterface CreateFake ( int something )
        {
            var fake = MockRepository.GenerateStub<IMyInterface> ();
            fake.Stub ( x => x.Property ).Return ( something );

            return fake;
        }
    }
}

I have been able to overcome the problem if I create a decorator class that wraps the generated stub:

public class Decorator : IMyInterface
{
    IMyInterface decorated;

    public Decorator ( IMyInterface decorated )
    {
        this.decorated = decorated;
    }

    public int Property
    {
        get
        {
            return decorated.Property;
        }
    }
}

And change the prior return fake; by return new Decorator ( fake );. Everything works fine then.

However this is a bit of a pain in my real scenario because my interface does not only have a single property as in this example but is more complex (and yes I know that VS2015 can generate the code for implementing through the decorated field, but that's not the point). Besides, it feels pointless to use RinhoMocks if I am going to end up creating an implementation of the interface I am wishing to not fully implement for my test purposes.

Anyway, what annoys me is that I do not understand why it does not work and therefore I ask:

Can anyone help me to understand why the code without the decorator did not work?

Thank you very much in advance.

Aucun commentaire:

Enregistrer un commentaire