All invocation on the mock must have a corresponding setup

12,962

I suppose the problem is in the following thing:

Contract.CentralServiceRequest req = new Contract.ConfigServer.GetDataVersionRequest(new ApplicationID("AMS", "QA"));

Proxy makes the call to get application version, but doesn't use this same request object (it probably creates another one with same parameters). Since it's different objects and mock is set up to expect the same, it fails.

Reasonable solution would be to expect any request of type CentralServiceRequest. I'm not well versed in Moq, but I suppose it's something like this:

ConnectionMock.Setup(f => f.SendRequest(ItExpr.IsAny<Contract.CentralServiceRequest>())).Returns(reply);

Hope this helps.

Share:
12,962
msjonathan
Author by

msjonathan

I' am an integration consultant with some experience in c#, vb.net.

Updated on June 04, 2022

Comments

  • msjonathan
    msjonathan almost 2 years

    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?