【问题标题】:ProtectSection force saveProtectSection 强制保存
【发布时间】:2014-03-10 19:07:01
【问题描述】:

如何强制下面的代码被下面的测试命中?

if (!section.SectionInformation.IsProtected)
{
     section.SectionInformation.ProtectSection(encryptionProvider);
}

问题是其他测试在下面的测试之前调用 Encrypt,所以代码永远不会进入上面的语句。

到目前为止,测试中的力节省似乎没有任何作用。

[TestCategory("Integration"), TestMethod]
public void TestProtectSectionOnlyHappensOnce() // integration test
{
    // Arrange
    IAppConfigEncryptor encryptor = new AppConfigEncryptor();
    bool expected = false;
    bool actual;

    // Act
    try
    {
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        var section = config.GetSection("appSettings");
        section.SectionInformation.SetRawXml("");
        config.Save(ConfigurationSaveMode.Full, true);

        encryptor.Encrypt("appSettings", "DataProtectionConfigurationProvider");
        encryptor.Encrypt("appSettings", "DataProtectionConfigurationProvider");
        actual = false;
    }
    catch (NullReferenceException)
    {
        actual = true;
    }

    // Assert
    Assert.AreEqual(expected, actual);
}

【问题讨论】:

    标签: c# .net integration-testing


    【解决方案1】:

    我会使用单独的配置文件,这样测试就不会受到干扰。多亏了所有测试都是独立的。

    1. 有一种方法OpenMappedExeConfiguration 可以让您打开一个非默认配置文件。
    2. 您可以在项目中添加第二个配置文件,并使用DeploymentItem 属性自动将其复制到测试目录。

    【讨论】:

      猜你喜欢
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多