c# - NUnit 或 Fluent Assertions 测试引用相等性?

标签 c# unit-testing reference nunit fluent-assertions

我正在使用 NUnit 2.6.2 + Fluent Assertions 2.0.1。

我想断言两个引用不指向同一个对象实例。我找不到一种干净的方式来表达这一点。

NUnitAssert.ReferenceEquals(ref1, ref2) - 但我找不到否定断言。

Fluent Assertions 中我找不到任何直接支持这种情况的东西。

我能做到的唯一方法是这样的:

NUnit: Assert.False(object.ReferenceEquals(ref1, ref2));

流畅:object.ReferenceEquals(ref1, ref2).Should().BeFalse();

就最小噪音而言,这两者似乎都不太理想。有没有更好的办法?

最佳答案

您可以使用 NotBeSameAs() 方法:

ref1.Should().NotBeSameAs(ref2);

它的文档说:

Asserts that an object reference refers to a different object than another object reference refers to.

关于c# - NUnit 或 Fluent Assertions 测试引用相等性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17741761/

相关文章:

c# - 针对第 3 方 dll 进行编程的最佳实践是什么?

javascript - 通过调用函数自动更新站点

c# - 在C#中将文件转换为二进制文件

unit-testing - 在 Grails 中对服务进行单元测试时如何模拟请求

c++ - 返回对指针的引用

reference - 在 rust 中保持干燥

c# - 停止其他脚本中的协程

javascript - 为什么 ava 无法比较对象列表和对象文字列表?

database - 使用 Spring 嵌入式数据库 API 在 HSQLDB 中创建存储过程

c++ - 引用 init 两次 cpp