Improve Documentation To Highlight Reliance on Implementation Details of Assert As Bad
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 35/100
- Issue 类型
- 文档
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- csharp
调研方向
首先阅读讨论 #168 以及此 issue 中关于改进文档的建议,然后找到相关的 NUnit assertion 文档。完成的标准是:记录 Assert 行为的变化,解释为什么不建议依赖实现细节,并展示 Assert.That 和 Assert.Throws 这两种替代方案。
由索引模型根据 Issue 内容生成。
描述
This is a continuation of this discussion: #168 I completely agree with @CharliePoole on this, but I would suggest that the documentation be improved to highlight the situation.
-- Begin Documentation Improvement Suggestion --
Behavior of Asserts
[Test]
public void TestAssertCatch()
{
try
{
Assert.That(true, Is.False);
}
catch
{
}
}
When looking at this example you may expect NUnit to Assert that true != false. However in versions of NUnit Prior to 3.6.0.0 this test would pass due to an implementation detail of Assert. In newer versions of NUnit this will Assert (and fail the test) as a design decision of the NUnit.org Team.
In cases where this is used, it was probably used in scenarios where better APIs could be leveraged.
Consider the following test which is trying to Assert that an Exception is thrown/not thrown:
public void TestException()
{
try
{
GuessTheNumber(42);
// This code only executes if no exception has occurred
Assert.Fail("An expected exception did not occur");
}
catch(Exception ex)
{
Assert.That(ex.Message, Is.EqualTo("The Answer"), "An expected exception did not occur");
}
}
public static void GuessTheNumber(int number)
{
if(number == 42)
{
throw new Exception("The Answer");
}
}
Instead this should be rewritten to use either Assert.That or Assert.Throws like so:
[Test]
public void TestExceptionAssertThat()
{
Assert.That(() => GuessTheNumber(42), Throws.InstanceOf<Exception>().With.Property("Message").EqualTo("The Answer"), "An expected exception did not occur");
}
Or Assert.Throws Style
[Test]
public void TestExceptionAssertThrows()
{
Exception ex = Assert.Throws<Exception>(() => GuessTheNumber(42), "An expected exception did not occur");
Assert.That(ex.Message, Is.EqualTo("The Answer"), "An expected exception did not occur");
}
-- End Documentation Improvement --
As Charlie mentioned:
I have asked the submitter of the issue to give a motivating example that would help us meet the need
It would be nice to hear what other scenarios end users are encountering that cause them to need to capture the exception of the Assert. The above example is just what we found in our code base during our 2.6.4->3.11.0 Convert.
Thank you for this tool; it is invaluable to us.
- 主要语言
- Dockerfile
- 星标
- 601
- 派生
- 159
- 平均合并
- 22 小时 18 分钟
- 30 天内合并 PR
- 6
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
nunit/docs 的其他 Issue
-
area:adapter type:newpage
难度 2/5 1-3 小时 新手友好度 82/100
-
Document why output from ASP.NET Core handlers (WebApplicationFactory) is not captured, and the workarounds可能已有人在做 @SeanKilleen 于 36 天前认领。 未关闭area:framework type:update
难度 2/5 1-3 小时 新手友好度 83/100
-
type:newpage
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 3/5 1-2 天 新手友好度 15/100
-
难度 4/5 3-5 天 新手友好度 35/100
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
audacity/audacity.github.io#118 ·
-
bug improve content
难度 1/5 1 小时以内 新手友好度 92/100
opensafely/documentation#2102 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 75/100
agentregistry-dev/website#69 ·
-
15.0
难度 2/5 1-3 小时 新手友好度 66/100
-
docs: add SECURITY.md vulnerability reporting policy可能已有人在做 @AK-Lmn 今天认领。 未关闭
难度 1/5 1-3 小时 新手友好度 72/100
flisk-sh/linux2wasm#12 ·
维护者通常 1 天内回复