Autofixture: Anpassen mit einer Anweisung Lambda funktioniert nicht - wird es erwartet?

Erstellt am 7. Juni 2018  ·  2Kommentare  ·  Quelle: AutoFixture/AutoFixture

Der folgende Test schlägt fehl:

public class UnitTest1
    {
        [Fact]
        public void Test1()
        {
            var fixture = new Fixture();
            fixture.Customize<PaymentReturnPayload>(c =>
            { // a statement lambda
                c.With(a => a.OperationAmount, "1");
                c.FromFactory(() => new PaymentReturnPayload(fixture.Create<int>().ToString()));
                return c;
            });
            var paymentReturnPayload = fixture.Create<PaymentReturnPayload>();
            Assert.True(int.TryParse(paymentReturnPayload.Control, out _));
        }
    }

    public class PaymentReturnPayload
    {
        public PaymentReturnPayload(string control)
        {
            Control = control;
        }

        public string OperationAmount { get; set; }

        public string Control
        {
            get;
        }
    }

Der Control-Parameter wird mit einer zufälligen Zeichenfolge anstelle von generiertem int gefüllt.

Version 4.0 - 4.4 (vorherige habe ich nicht getestet)
Netzkern 2.0 - 2.1

Das funktioniert gut

[Fact]
        public void Test1()
        {
            var fixture = new Fixture();
            fixture.Customize<PaymentReturnPayload>(c =>
                c.FromFactory(() => new PaymentReturnPayload(fixture.Create<int>().ToString())));
            var paymentReturnPayload = fixture.Create<PaymentReturnPayload>();
            Assert.True(int.TryParse(paymentReturnPayload.Control, out _));
        }

Es sieht so aus, als ob das Anpassen mit einem Anweisungs-Lambda nicht funktioniert.
Ist es ein erwartetes Verhalten?
Und wenn ja, sollte es nicht einer Dokumentation hinzugefügt werden? Ich habe viel Zeit verloren, um herauszufinden, was los ist...

question

Alle 2 Kommentare

@progala2 Danke, dass hast .

Das vorhandene Verhalten ist beabsichtigt. Der Grund dafür ist, dass die Methoden FromFactory , With (und Without ) unveränderlich sind, also das zugrunde liegende Objekt nicht ändern. Stattdessen geben sie ein neues Objekt mit konfiguriertem Graph zurück.

Der Grund, warum der erste Test nicht funktioniert, ist, dass Sie das Ergebnis der Aufrufe nicht verwenden, sodass alle angewendeten Änderungen verworfen werden. Sobald Sie den Code richtig umgeschrieben haben, funktioniert er auch mit Anweisungen:

```c#
[Tatsache]
öffentlicher ungültiger Test1()
{
var Fixture = new Fixture();
Befestigung.Anpassen(c =>
{
// eine Anweisung Lambda
var res = c.FromFactory(() => new PaymentReturnPayload(fixture.Create().ToString()));
res = res.With(a => a.OperationAmount, "1");
Rückkehr res;
});

var paymentReturnPayload = fixture.Create<PaymentReturnPayload>();
Assert.True(int.TryParse(paymentReturnPayload.Control, out _));

}


But as you might notice, it's easier to write this code with lambda.

P.S. By the way, you can slightly improve the final code (notice the `FromFactory` has overload taking extra values necessary for the object construction):
```c#
[Fact]
public void Test3()
{
    var fixture = new Fixture();
    fixture.Customize<PaymentReturnPayload>(c =>
        c.FromFactory((int control) => new PaymentReturnPayload(control.ToString())));

    var paymentReturnPayload = fixture.Create<PaymentReturnPayload>();
    Assert.True(int.TryParse(paymentReturnPayload.Control, out _));
}

Ich sehe, sie sind unveränderlich - es ist jetzt viel klar.
Vielen Dank für Ihre Antwort und einige zusätzliche Tipps!

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen

Verwandte Themen

zvirja picture zvirja  ·  8Kommentare

ploeh picture ploeh  ·  3Kommentare

josh-degraw picture josh-degraw  ·  4Kommentare

zvirja picture zvirja  ·  4Kommentare

Eldar1205 picture Eldar1205  ·  5Kommentare