【发布时间】: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