Search code examples
vb.netvisual-studio-2008rhino-mocks

Using Rhino Mocks' Expect.Call for a Mock Property in VB.NET


The following C# code works just fine and the test passes as expected.

using NUnit.Framework;
using Rhino.Mocks;

namespace RhinoMocksTesting
{
    public interface ITesting
    {
        string Test { get; }
    }

    [TestFixture]
    public class MocksTest
    {

        [Test]
        public void TestMockExpect()
        {
            var mocks = new MockRepository();
            var testMock = mocks.StrictMock<ITesting>();
            Expect.Call(testMock.Test).Return("testing");
            mocks.ReplayAll();
            Assert.AreEqual("testing", testMock.Test);
        }
    }
}

However, trying to do the same thing in VB.NET won't even compile!

Imports NUnit.Framework
Imports Rhino.Mocks

Public Interface ITesting
    ReadOnly Property Test() As String
End Interface

<TestFixture()> _
Public Class MocksTest

    <Test()> _
    Public Sub TestMockExpect()
        Dim mocks = New MockRepository
        Dim testMock = mocks.StrictMock(Of ITesting)()
        Expect.Call(testMock.Test).Return("testing")
        mocks.ReplayAll()
        Assert.AreEqual("testing", testMock.Test)
    End Sub

End Class

The Expect.Call line produces the following build error: "Overload resolution failed because no accessible 'Expect' accepts this number of arguments."

What is the proper way to use Expect.Call with a mocked property in VB.NET? I've seen a couple posts that say Rhino Mocks works better in VB10, but I'm stuck with Visual Studio 2008 for this current project.


Solution

  • Try

    Rhino.Mocks.Expect.Call(testMock.Test).Return("testing")
    

    Source

    Now we switch to Rhino mocks 3.5 and see that we get an error on Expect saying that the signature is not correct. No worry this is because it is choosing the wrong Expect. It is namely trying to use the extension method there. Just add Rhino.Mocks. before the Expect and all is well again. Look how the imports doesn't do the same thing.