Questions regarding fluent syntax design for assertions
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Anfängerfreundlichkeit
- 20/100
- Issue-Typ
- Dokumentation
- Klarheit
- Muss geklärt werden
- Aktivitätsstatus
- Veraltet
- Tech-Stack
- csharp
- Bereich
- documentation, testing-qa
Rechercherichtung
Beginnen Sie mit der Prüfung der in den Beispielen des Issues referenzierten Dokumentation zu Assertions und Constraints, insbesondere zu Has, Contains und lambda-basiertem Matching. Vergleichen Sie die dokumentierte NUnit-Syntax mit den Fällen von FluentAssertions und klären Sie anschließend die Designbegründung, unterstützte Entsprechungen und Einschränkungen, sodass jedes Beispiel eine eindeutig dokumentierte Antwort hat.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Coming from XUnit + FluentAssertions, I must say I love the TestCase feature of NUnit.
At the same time I feel that the assertion syntax seems all over the place. The fluent attempt mostly applies to reading the assertion, but definitely not to writing them, because Intellisense gives many false suggestions. And for the number of constraints listed in documentation the possibilities still seem rather limited.
There's nothing preventing me from using FluentAssertions, but I'm trying to understand the philosophy in the current assertion/constraint design in NUnit, especially since a lot of work seems to have gone into it.
First example of clunkiness: why does the Has class have a 'Length' and 'Count' which only work on classes with that exact property name? This leads to tests seemingly perfect by reading the code, but still failing at runtime because a collection was of an unexpected type. It's just confusing while there is a syntax that always works...
[Test]
public void Test_ItemCount()
{
var MyArray = new[] { 1, 2, 3 };
var MyCollection = new List<int> { 1, 2, 3, };
var MyEnumerable = Enumerable.Range(1, 3);
// NUnit assertions
Assert.That(MyArray, Has.Exactly(3).Items);
Assert.That(MyArray, Has.Length.EqualTo(3));
//Assert.That(MyArray, Has.Count.EqualTo(3)); // System.ArgumentException : Property Count was not found on System.Int32[]. (Parameter 'name')
Assert.That(MyCollection, Has.Exactly(3).Items);
Assert.That(MyCollection, Has.Count.EqualTo(3));
//Assert.That(MyCollection, Has.Length.EqualTo(3)); // System.ArgumentException : Property Length was not found on System.Collections.Generic.List`1[System.Int32]. (Parameter 'name')
Assert.That(MyEnumerable, Has.Exactly(3).Items);
//Assert.That(MyEnumerable, Has.Count.EqualTo(3)); // System.ArgumentException : Property Count was not found on System.Linq.Enumerable+RangeIterator. (Parameter 'name')
//Assert.That(MyEnumerable, Has.Length.EqualTo(3)); // System.ArgumentException : Property Length was not found on System.Linq.Enumerable+RangeIterator. (Parameter 'name')
// FluentAssertions
MyArray.Should().HaveCount(3);
MyCollection.Should().HaveCount(3);
MyEnumerable.Should().HaveCount(3);
}
Other examples of clunkiness when it comes to Intellisense:
Assert.That(MyArray, Has.None.Zero); // Pass
Assert.That(MyArray, Is.Not.Zero); // Pass - but probably means something entirely different
// Why do the following even compile?
//Assert.That(MyArray, Has.Exactly(3).Items.All.Exist); // Expected: IEnumerable But was: Int32 (Parameter 'actual')
//Assert.That(MyArray, Has.Exactly(3).Items.All.Not.EqualTo(0)); // Expected: IEnumerable But was: Int32 (Parameter 'actual')
//Assert.That(MyArray, Has.Exactly(3).Items.And.All.All.All.Not.EqualTo(0)); // Expected: IEnumerable But was: Int32 (Parameter 'actual')
Finally, I'm wondering whether it's possible to (1) match using a lambda expression, and (2) drill down into a match to assert its properties. Is there something equivalent in NUnit?
// FluentAssertions
MyArray.Should().ContainSingle(x => x > 2).Which.Should().Be(3);
MyArray.Should().ContainSingle(x => x > 2).Which.Should().Be(3).And.NotBe(4);
var colors = new[] { Color.Red };
colors.Should().ContainSingle(x => x.IsKnownColor).Which.R.Should().Be(255);
// NUnit expected syntax
//Assert.That(MyArray, Contains.Single(x => x > 2) (...) // Syntax error
//Assert.That(MyArray, Has.One.Match(x => x > 2) (...) // Syntax error
//Assert.That(colors, Has.Exactly(1).Items.With.R (...) // Syntax error
- Vorherrschende Sprache
- Dockerfile
- Sterne
- 601
- Forks
- 160
- Ø Merge
- 22 Std. 18 Min.
- Gemergte PRs (30 T.)
- 6
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus nunit/docs
-
area:adapter type:newpage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
-
Document why output from ASP.NET Core handlers (WebApplicationFactory) is not captured, and the workaroundsEvtl. vergeben @SeanKilleen hat das vor 36 Tagen übernommen. Offenarea:framework type:update
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 83/100
-
type:newpage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
-
Dependency DashboardOffen
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 15/100
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 35/100
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 73/100
google/elephant-goldfish-model#586 ·
Maintainer antworten meist innerhalb von 2 Tagen
-
documentation Entwickler
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 89/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
memtomem/memtomem-stm#1657 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Help with the docsOffendocumentation good first issue
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 76/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
lightninglabs/layertree#28 ·