Questions regarding fluent syntax design for assertions
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 20/100
- Loại issue
- Tài liệu
- Độ rõ ràng
- Cần làm rõ
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- csharp
- Lĩnh vực
- documentation, testing-qa
Hướng nghiên cứu
Bắt đầu bằng việc xem xét tài liệu về assertion và constraint được các ví dụ trong issue tham chiếu, đặc biệt là Has, Contains và việc so khớp dựa trên lambda. So sánh cú pháp NUnit được tài liệu hóa với các trường hợp của FluentAssertions, sau đó làm rõ cơ sở thiết kế, các tương đương được hỗ trợ và các giới hạn để mỗi ví dụ đều có một câu trả lời được tài liệu hóa, không mơ hồ.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- Dockerfile
- Star
- 601
- Fork
- 160
- Merge trung bình
- 22 giờ 18 phút
- Pull request đã merge (30 ngày)
- 6
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của nunit/docs
-
Document the adapter's agent output settings (AgentMode, ShowStdout, ShowStderr, StackTrace)Đang mởarea:adapter type:newpage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
-
Document why output from ASP.NET Core handlers (WebApplicationFactory) is not captured, and the workaroundsCó thể đã có người làm @SeanKilleen đã nhận 36 ngày trước. Đang mởarea:framework type:update
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
-
type:newpage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Dependency DashboardĐang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 15/100
-
Move console/engine wiki to docsĐang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 73/100
google/elephant-goldfish-model#586 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
documentation Entwickler
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 89/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
memtomem/memtomem-stm#1657 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Help with the docsĐang mởdocumentation good first issue
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 76/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
lightninglabs/layertree#28 ·