微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NUnit 测试互连?

如何解决NUnit 测试互连?

我有一个 NUnit 测试类的以下代码,用于测试 TodoList 应用程序中的两个管理类(用于 Comments 和 TodoItems):

public class CommentManagerTests
    {
        private DbContextOptions<TodoListDbContext> inMemoryContextOptions =
        new DbContextOptionsBuilder<TodoListDbContext>().UseInMemoryDatabase("WorkshopTodoList").Options;

        CommentManager commentManagerToTest;
        TodoItemmanager itemmanagerToTest;

        [SetUp]
        public void Setup()
        {
            var context = new TodoListDbContext(inMemoryContextOptions);

            commentManagerToTest = new CommentManager(context);
            itemmanagerToTest = new TodoItemmanager(context);

            itemmanagerToTest.AddItem(new AddItemDTO { Description = "Description 1"});
            itemmanagerToTest.AddItem(new AddItemDTO { Description = "Description 2"});
            itemmanagerToTest.AddItem(new AddItemDTO { Description = "Description 3"});

            System.Console.WriteLine("SetUp");
        }

        [Test]
        public void AddComment()
        {
            System.Console.WriteLine("Add");

            var commentToAdd = new AddCommentToItemDTO { Body = "Not Ok!",ParentPostId = 1 };
            var commentAdded = commentManagerToTest.AddComment(commentToAdd);
            Assert.AreEqual(commentToAdd.Body,commentAdded.Body);
            Assert.AreEqual(commentToAdd.ParentPostId,commentAdded.ParentPostId);


        }

        [Test]
        public void GetAllCommentsOfItem()
        {
            System.Console.WriteLine("Get");

            var allComments = commentManagerToTest.GetAllCommentsOfItem(2);

            Assert.IsEmpty(allComments);

            commentManagerToTest.AddComment(new AddCommentToItemDTO { Body = "Frumos peisaj!",ParentPostId = 1 });
            commentManagerToTest.AddComment(new AddCommentToItemDTO { Body = "Frumos cantec!",ParentPostId = 1 });
            commentManagerToTest.AddComment(new AddCommentToItemDTO { Body = "Frumos album!",ParentPostId = 1 });
            commentManagerToTest.AddComment(new AddCommentToItemDTO { Body = "Frumoasa haina!",ParentPostId = 3 });

            allComments = commentManagerToTest.GetAllCommentsOfItem(1);

            //the exception appears hear (4 instead of 3)
            Assert.AreEqual(3,allComments.Count);


            allComments = commentManagerToTest.GetAllCommentsOfItem(3);

            Assert.AreEqual(1,allComments.Count);

        }

问题是第二个测试函数Assert.AreEqual(3,allComments.Count) 上失败。似乎带有描述“不好!”的评论从第一个函数仍然在上下文中。 不是应该在第二个测试函数之前调用SetUp函数后消失吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?