All invocation on the mock must have a corresponding setup

Viewed 13619

I have some legacy code I want to unit test. I created a first moq test but I am getting the following exception:

Moq.MockException:IConnection.SendRequest(ADF.Messaging.Contract.ConfigServer.GetDataVersionRequest) invocation failed with mock behavior Strict. All invocations on the mock must have a corresponding setup.

Important pieces of code:

Property on class:

Public Property Connection() As IConnection
    Get
        Return _connection
    End Get
    Set(ByVal value As IConnection)
        _connection = value
    End Set
End Property

The method that should be tested: (_connection) is a actually a class that creates a tcp socket and I want to mock that property so the SendRequest returns what I want.

Public Function GetVersion(ByVal appID As Contract.ApplicationID) As Contract.DataVersion
    EnsureConnected()
    Dim req As GetDataVersionRequest = New GetDataVersionRequest(appID)

    Dim reply As CentralServiceReply = _connection.SendRequest(req) //code I want to mock
    Utils.Check.Ensure(TypeOf reply Is GetDataVersionReply, String.Format("Unexpected type: {0}, expected GetDataVersionReply!", reply.GetType()))

    Dim version As Contract.DataVersion = CType(reply, GetDataVersionReply).Version
    version.UpgradeOwners()
    If (Not version.IsSupported) Then
        Return Contract.DataVersion.UNSUPPORTED
    End If

    Return version
End Function

Test Method:

[TestMethod]
public void TestMethod2()
{
    Contract.CentralServiceRequest req = new Contract.ConfigServer.GetDataVersionRequest(new ApplicationID("AMS", "QA"));

    DataVersion v = new DataVersion();
    v.AppVersion = "16";
    CentralServiceReply reply = new GetDataVersionReply(v);

    var ConnectionMock = new Mock<IConnection>(MockBehavior.Strict);
    ConnectionMock.Setup(f => f.SendRequest(req)).Returns(reply);

    var proxy = new ConfigServerProxy(new ApplicationID("AMS", "QA"), "ws23545", 8001);
    proxy.Connection = ConnectionMock.Object; //assign mock object

    DataVersion v2 = proxy.GetVersion(new ApplicationID("AMS", "QA"));
    Assert.AreEqual(v.AppVersion, v2.AppVersion);
}

When I debug the unit test I see that when proxy.GetVersion is executed on the line _connection.SendRequest we get the error. Also when I watch the variable (_connection) in the watch window I see it's the moq object. So I suppose that property assignment went well.

Does anybody see where I went wrong?

1 Answers
Related