问题描述
我有一个 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 (将#修改为@)