Alle den Aufruf auf der Attrappe muss eine entsprechende setup

Habe ich einige legacy-code ich möchte die unit-Tests. Ich erstellte eine erste moq testen, aber ich bin immer folgende exception:

Moq.MockException:IConnection.SendRequest(ADF.Messaging.Vertrag.ConfigServer.GetDataVersionRequest)
Aufruf fehlgeschlagen mit mock-Verhalten Streng. Alle Aufrufe auf die
mock muss eine entsprechende setup.

Wichtige Teile des Codes:

Eigenschaft Klasse:

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

Die Methode, die getestet werden sollen: (_connection) ist tatsächlich eine Klasse, erstellt einen tcp-socket und ich will spotten, dass Eigentum, so die SendRequest gibt, was ich will.

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 Methode:

[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);
}

Wenn ich Debuggen, unit-test sehe ich, dass, wenn der proxy.GetVersion ausgeführt wird, auf der Linie _connection.SendRequest wir den Fehler. Auch wenn ich mir die variable (_connection) im watch-Fenster sehe ich, es ist das moq-Objekt. Also ich nehme an, dass Immobilien, die Zuordnung ging gut.

Weiß jemand sehen, wo ich schief gelaufen?

InformationsquelleAutor msjonathan | 2012-08-16

Schreibe einen Kommentar