nunit - 使用 Assert 比较两个对象

标签 nunit testing collections assert unit-testing

为我的项目编写测试用例,我需要的一个测试是测试删除。这可能不是正确的方法,但我偶然发现了一些对我来说没有意义的东西。

代码是这样的:

[Test]
private void DeleteFruit()
{
    BuildTestData();
    var f1 = new Fruit("Banana",1,1.5);
    var f2 = new Fruit("Apple",1,1.5);
    fm.DeleteFruit(f1,listOfFruit);
    Assert.That(listOfFruit[1] == f2);
}

现在,我在第 5 行创建的水果对象是我知道在删除 f1 后应该位于该位置(具有此特定数据集)的对象。

此外,如果我坐下来调试,并手动比较对象 listOfFruit[1] 和 f2,它们是相同的。但是那条 Assert 行失败了。给了什么?

编辑:

获取以下内容:

- Assert.Equals不应该用于断言

- Assert.AreEqual(fruit1, fruit2); Failed: Expected <FruitProject.Fruit> But was: <FruitProject.Fruit>

-两者fruit1.Equals(fruit2)fruit1==fruit2失败??

最佳答案

==比较引用,因为listOfFruit[1]和f2的引用不一样,所以失败

C# difference between == and Equals()

关于nunit - 使用 Assert 比较两个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2778331/

相关文章:

testing - SonarQube 单元测试工具

java - 在 Spring boot 测试中,如何创建一个 bean 并通过 @Autowired 注入(inject)?

c# - NUnit 的一次性初始化

c# - 异步单元测试未按预期工作

unit-testing - 我创建了NUnit测试,现在如何运行它们?

asp.net-mvc - 带有存储库的静态实用程序类的依赖性问题(.NET MVC 项目)

Angular 2 : Replace injected Renderer with jasmine spy object in component shallow tests

ios - 在 CollectionView 中关闭已使用的单元格并打开未使用的单元格

c# - 如何使 ObservableCollection 中的项目独一无二?

java - 将2张 map 放入1张 map