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.JdbcOdbc...