【发布时间】:2010-08-30 19:45:53
【问题描述】:
我是单元测试的新手。我想做如下事情:
[Test]
[ExpectedException(ExceptionType = typeof(Exception))]
public void TestDeleteCategoryAssociatedToTest()
{
Category category = CategoryHelper.Create("category", Project1);
User user;
Test test1 = IssueHelper.Create(Project1, "summary1", "description1", user);
test1.Category = category;
category.Delete(user);
Assert.IsNotNull(Category.Load(category.ID));
Assert.IsNotNull(Test.Load(test1.ID).Category);
}
我的目的是通过执行Assert.IsNotNull()... 来测试该类别是否未被删除,但由于它引发了异常,因此它没有到达那段代码。知道如何改进上述测试吗?
实际上,在我的 API 中,如果类别与测试相关联,我会抛出异常... 我的 sn-p 是:
IList<Test> tests= Test.LoadForCategory(this);
if (tests.Count > 0)
{
throw new Exception("Category '" + this.Name + "' could not be deleted because it has items assigned to it.");
}
else
{
base.Delete();
foreach (Test test in tests)
{
test.Category = null;
}
}
【问题讨论】:
-
您是否要删除该类别?我无法理解您的代码流程。 category.Delete(user) 做什么?这就是问题所在吗?
-
我认为他正在尝试检查,删除异常没有被不必要地抛出
标签: c# unit-testing