使用Microsoft Fakes将VS 2010的Pex TestMethods转换为VS2013

时间:2015-05-08 15:44:22

标签: c# unit-testing microsoft-fakes pex

我正在尝试将下面的pex测试方法转换为正常的单元测试。虽然我打算在需要的地方使用Microsoft Fakes,但我想先了解一些事情。

[TestMethod]
[PexGeneratedBy(typeof(ErrorLogTest))]
public void Initialize139()
{
    ErrorLog errorLog;
    NameValueCollection nameValueCollection;
    errorLog = new ErrorLog();
    errorLog.MyProperty = false;
    KeyValuePair<string, string>[] keyValuePairs = new KeyValuePair<string, string>[5];
    KeyValuePair<string, string> s0 = new KeyValuePair<string, string>("", "");
    keyValuePairs[0] = s0;
    KeyValuePair<string, string> s1 = new KeyValuePair<string, string>("", "");
    keyValuePairs[1] = s1;
    KeyValuePair<string, string> s2 = new KeyValuePair<string, string>("", "");
    keyValuePairs[2] = s2;
    KeyValuePair<string, string> s3 = new KeyValuePair<string, string>("", "");
    keyValuePairs[3] = s3;
    KeyValuePair<string, string> s4 = new KeyValuePair<string, string>("", "");
    keyValuePairs[4] = s4;
    nameValueCollection = PexFactories.CreateNameValueCollection(keyValuePairs);
    this.Initialize(errorLog, "", nameValueCollection);
    Assert.IsNotNull((object)errorLog);
    Assert.AreEqual<bool>(false, errorLog.MyProperty);
}

我已将其转换为如下所示的简单单元测试:

[TestMethod]
public void Initialize1390()
{
    ErrorLog errorLog;
    NameValueCollection nameValueCollection = new NameValueCollection();
    errorLog = new ErrorLog();
    errorLog.MyProperty = false;
    KeyValuePair<string, string>[] keyValuePairs = new KeyValuePair<string, string>[5];
    KeyValuePair<string, string> s0 = new KeyValuePair<string, string>("", "");
    keyValuePairs[0] = s0;
    KeyValuePair<string, string> s1 = new KeyValuePair<string, string>("", "");
    keyValuePairs[1] = s1;
    KeyValuePair<string, string> s2 = new KeyValuePair<string, string>("", "");
    keyValuePairs[2] = s2;
    KeyValuePair<string, string> s3 = new KeyValuePair<string, string>("", "");
    keyValuePairs[3] = s3;
    KeyValuePair<string, string> s4 = new KeyValuePair<string, string>("", "");
    keyValuePairs[4] = s4;                
    errorLog.Initialize("", nameValueCollection);
    Assert.IsNotNull((object)errorLog);
    Assert.AreEqual<bool>(false, errorLog.MyProperty);
}

我在这里有两个问题:

  • 在从pexmethod到testmethod的转换过程中,我是否会失去任何方案?
  • 我在nameValueCollection中看到了计数。返回的计数值是1.这是因为我插入的所有KeyValuePair都是相同的吗?

1 个答案:

答案 0 :(得分:1)

您的新测试似乎缺少将keyValuePairs数组的内容复制到nameValueCollection集合中的步骤。我相信这是在这条线的原始测试中完成的:

nameValueCollection = PexFactories.CreateNameValueCollection(keyValuePairs);