我正在编写的集成测试有问题。我需要在测试结束时执行清除操作,方法是删除在测试过程中添加的Outlook中的类别(从可用类别列表中)。对于“已归档”类别,我按如下操作:
using Microsoft.Office.Interop.Outlook;
var outlookApplication = new Application();
outlookApplication.Session.Categories.Remove("Filed");
这无法删除类别,但不能一致。当我调试代码时,它可以工作,但在运行测试时却不能。
更新:
这是所有测试代码:
[TestFixture]
public class BootstrapperTest
{
private bool containsFiled;
private bool containsPending;
private Application outlookApplication = new Application();
[Test]
public void CanCreateFiledCategory()
{
var bootstrapper = new Bootstrapper();
bootstrapper.LoadCategoriesIntoOutlook(outlookApplication);
var filedCategoryFound = outlookApplication.Session.Categories.Cast<Category>().Any(category => category.Name == "Filed");
Assert.That(filedCategoryFound, Is.EqualTo(true));
}
[Test]
public void CanCreatePendingCategory()
{
var bootstrapper = new Bootstrapper();
bootstrapper.LoadCategoriesIntoOutlook(outlookApplication);
var pendingCategoryFound = outlookApplication.Session.Categories.Cast<Category>().Any(category => category.Name == "Pending");
Assert.That(pendingCategoryFound, Is.EqualTo(true));
}
[SetUp]
public void Setup()
{
containsFiled = DoesCategoryNameExist(outlookApplication.Session.Categories, "Filed");
containsPending = DoesCategoryNameExist(outlookApplication.Session.Categories, "Pending");
}
[TearDown]
public void TearDown()
{
RemoveAllCategoriesFromOutlook();
}
private bool DoesCategoryNameExist(Categories categoryList, string categoryName)
{
return categoryList.Cast<Category>().Any(category => category.Name == categoryName);
}
private void RemoveAllCategoriesFromOutlook()
{
var containsFiledNow = DoesCategoryNameExist(outlookApplication.Session.Categories, "Filed");
var containsPendingNow = DoesCategoryNameExist(outlookApplication.Session.Categories, "Pending");
if (!containsFiled && containsFiledNow) outlookApplication.Session.Categories.Remove("Filed");
if (!containsPending && containsPendingNow) outlookApplication.Session.Categories.Remove("Pending");
}
}
它正在测试的方法:
public void LoadCategoriesIntoOutlook(Application outlookApplication)
{
var categories = outlookApplication.Session.Categories;
var filedCategoryNameExists = DoesCategoryNameAlreadyExist(categories, FiledCategoryName);
var pendingCategoryNameExists = DoesCategoryNameAlreadyExist(categories, PendingCategoryName);
var filedCategoryColourIsUsed = IsCategoryColorAlreadyUsed(categories, FiledCategoryColor);
var pendingCategoryColourIsUsed = IsCategoryColorAlreadyUsed(categories, PendingCategoryColor);
if (!filedCategoryNameExists)
{
if (filedCategoryColourIsUsed)
{
var categoryToBeChangedToFiled =
categories.Cast<Category>()
.Where(category => category.Color == FiledCategoryColor)
.FirstOrDefault();
categoryToBeChangedToFiled.Name = FiledCategoryName;
}
else
{
categories.Add(FiledCategoryName, FiledCategoryColor);
}
}
if (!pendingCategoryNameExists)
{
if (pendingCategoryColourIsUsed)
{
var categoryToBeChangedToPending =
categories.Cast<Category>()
.Where(category => category.Color == PendingCategoryColor)
.FirstOrDefault();
categoryToBeChangedToPending.Name = PendingCategoryName;
}
else
{
categories.Add(PendingCategoryName, PendingCategoryColor);
}
}
}
最佳答案
您应该记录是否甚至通过调用Categories.Remove
调用Trace.TraceInformation()
,以查看在非DEBUG模式下运行时分支条件是否存在错误。 Categories.Remove
确实可以可靠地工作,这一定是您的情况中存在错误。如果是这样,您将看不到日志记录语句。
private void RemoveAllCategoriesFromOutlook()
{
var containsFiledNow = DoesCategoryNameExist(outlookApplication.Session.Categories, "Filed");
var containsPendingNow = DoesCategoryNameExist(outlookApplication.Session.Categories, "Pending");
if (!containsFiled && containsFiledNow)
{
outlookApplication.Session.Categories.Remove("Filed");
Trace.TraceInformation("Deleted Filed Category!")
}
if (!containsPending && containsPendingNow)
{
outlookApplication.Session.Categories.Remove("Pending");
Trace.TraceInformation("Deleted Pending Category!")
}
}
同样,由于您是根据颜色创建的
Categories
(请参见filedCategoryColourIsUsed
),即使通过重命名(containsFiled
)而不是通过Category.Name = "Filed"
创建了Categories.Add()
,DoesCategoryNameExist
仍可能返回FALSE。您的问题是Setup
在您的Setup()
测试治具中不考虑类别颜色。以下代码应解决此问题...[SetUp]
public void Setup()
{
containsFiled = DoesCategoryNameExist(outlookApplication.Session.Categories, "Filed") || IsCategoryColorAlreadyUsed(categories, FiledCategoryColor);
containsPending = DoesCategoryNameExist(outlookApplication.Session.Categories, "Pending") || IsCategoryColorAlreadyUsed(categories, PendingCategoryColor);
}