如何解决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 举报,一经查实,本站将立刻删除。