C# Application.Session.Categories.Remove不一致地删除类别
我写的集成测试有问题。我需要在测试结束时通过删除Outlook中我在测试期间添加的类别(从可用类别列表中)来执行清理。对于“已归档”类别,我按如下方式执行此操作:C# Application.Session.Categories.Remove不一致地删除类别,c#,c#-4.0,interop,outlook,nunit,C#,C# 4.0,Interop,Outlook,Nunit,我写的集成测试有问题。我需要在测试结束时通过删除Outlook中我在测试期间添加的类别(从可用类别列表中)来执行清理。对于“已归档”类别,我按如下方式执行此操作: using Microsoft.Office.Interop.Outlook; var outlookApplication = new Application(); outlookApplication.Session.Categories.Remove("Filed"); 这无法删除该类别,但并不一致。当我调试代码时,它可以工
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");
}
}
[TestFixture]
公共类引导测试
{
私人布尔包含文件;
私人支出;
私有应用程序outlookApplication=新应用程序();
[测试]
公共无效CanCreateFiledCategory()
{
var bootstrapper=new bootstrapper();
bootstrapper.LoadCategoriesIntoOutlook(Outlook应用程序);
var filedCategoryFound=outlookApplication.Session.Categories.Cast().Any(category=>category.Name==“field”);
Assert.That(filedCategoryFound,Is.EqualTo(true));
}
[测试]
公共无效CanCreatePendingCategory()
{
var bootstrapper=new bootstrapper();
bootstrapper.LoadCategoriesIntoOutlook(Outlook应用程序);
var pendingCategoryFound=outlookApplication.Session.Categories.Cast().Any(category=>category.Name==“待定”);
断言(pendingCategoryFound,Is.EqualTo(true));
}
[设置]
公共作废设置()
{
containsFiled=DoesCategoryNameExist(outlookApplication.Session.Categories,“归档”);
ContainExpension=DoesCategoryNameExist(outlookApplication.Session.Categories,“待定”);
}
[撕裂]
公共无效拆卸()
{
从Outlook()中删除所有类别;
}
私有布尔DoesCategoryName存在(类别CategorityList、字符串categoryName)
{
返回categoryList.Cast().Any(category=>category.Name==categoryName);
}
私有void从Outlook()中删除所有类别
{
var containsFiledNow=DoesCategoryNameExist(outlookApplication.Session.Categories,“归档”);
var containsPendingNow=DoesCategoryNameExist(outlookApplication.Session.Categories,“待定”);
如果(!containsFiled&&containsFiledNow)outlookApplication.Session.Categories.Remove(“归档”);
如果(!containExpanding&&containsPendingNow)outlookApplication.Session.Categories.Remove(“待定”);
}
}
以及它正在测试的方法:
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);
}
}
}
public void loadcategoriesintoolk(应用程序outlookApplication)
{
var categories=outlookApplication.Session.categories;
var filedCategoryNameExists=DoesCategoryNameAlreadyExist(类别,FiledCategoryName);
var pendingCategoryNameExists=DoesCategoryNameAlreadyExist(类别,PendingCategoryName);
var filedCategoryColourIsUsed=IsCategoryColorAlreadyUsed(类别,FiledCategoryColor);
var pendingCategoryColourIsUsed=IsCategoryColorAlreadyUsed(类别,PendingCategoryColor);
如果(!filedCategoryNameExists)
{
如果(使用FiledCategoryColor)
{
变量类别更改为=
类别
.Where(category=>category.Color==FiledCategoryColor)
.FirstOrDefault();
categoryToBeChangedToFiled.Name=FiledCategoryName;
}
其他的
{
添加(FiledCategoryName、FiledCategoryColor);
}
}
如果(!PendingCategoryName存在)
{
如果(使用PendingCategoryColor)
{
变量类别更改打开=
类别
.Where(category=>category.Color==PendingCategoryColor)
.FirstOrDefault();
CategoryToBeChangedOpening.Name=PendingCategoryName;
}
其他的
{
添加(PendingCategoryName,PendingCategoryColor);
}
}
}
您应该记录是否调用了类别。甚至调用了Remove
(通过Trace.TraceInformation()
)以查看在非调试模式下运行时分支条件中是否存在错误<代码>类别。如果删除工作可靠,则在您的情况下一定是错误。如果是这样,您将看不到日志记录语句
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!")
}
}
此外,由于您根据颜色创建类别(请参见filedCategoryColourIsUsed
),containsFiled
可能返回FALSE,即使您是通过重命名(Category.Name=“field”
)而不是通过Categories.Add()
创建的。你的问题是<代码>不存在,在测试夹具中不考虑类别颜色。下面的Setup()
代码应该可以更正此问题
[SetUp]
public void Setup()
{
containsFiled = DoesCategoryNameExist(outlookApplication.Session.Categories, "Filed") || IsCategoryColorAlreadyUsed(categories, FiledCategoryColor);
containsPending = DoesCategoryNameExist(outlookApplication.Session.Categories, "Pending") || IsCategoryColorAlreadyUsed(categories, PendingCategoryColor);
}
请共享上下文的测试代码。可能是您正在使用相同的名称添加多个类别,存在COM异常,或者您检查已删除类别的方式不正确。删除via只会删除第一个匹配的
类别
@SliverNinja-我已按要求添加了相关代码