c# - Application.Session.Categories.Remove 不会一致地删除类别

标签 c# c#-4.0 interop outlook nunit

我正在编写的集成测试有问题。我需要在测试结束时通过删除我在测试期间添加的 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!")
        }
}

此外,由于您根据颜色创建类别(请参阅filedCategoryColourIsUsed),containsFiled 可能会返回 FALSE ,即使您是通过重命名 (Category.Name = "Filed") 而不是通过 Categories.Add() 创建的。您的问题是 DoesCategoryNameExistSetup 期间不考虑类别颜色测试夹具。下面的 Setup() 代码应该可以解决这个问题...

[SetUp]
public void Setup()
{
    containsFiled = DoesCategoryNameExist(outlookApplication.Session.Categories, "Filed") || IsCategoryColorAlreadyUsed(categories, FiledCategoryColor);
    containsPending = DoesCategoryNameExist(outlookApplication.Session.Categories, "Pending") || IsCategoryColorAlreadyUsed(categories, PendingCategoryColor);
}

关于c# - Application.Session.Categories.Remove 不会一致地删除类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11205382/

相关文章:

c# - 在服务器端访问客户端变量

javascript - 在 ASP.NET 服务器上缩小 CSS 和 JavaScript 的最佳方法是什么?

c#-4.0 - 如何在 ToTraceString() 不起作用的情况下获得由 Entity Framework 生成的真实 T-SQL

c# - 未经检查的 block 不适用于 BigInteger?

c# - 使用 C/C++ 编写 DLL 以实现 .Net 互操作性

c# - RedirectToAction 在特定操作(进程)中不起作用

c# - 如何在 Xamarin 中创建可重用的图标菜单

c# - 如何匹配区分大小写的用户名和密码

opengl - OpenCL/OpenGL 与多个 GPU 互操作

.net - Interop:在部署 VB6 + .NET 组件时,是否需要 .TLB?