使用 Microsoft Fakes 将 VS 2010 的 Pex TestMethods 转换为 VS2013
Convert the Pex TestMethods of VS 2010 to the VS2013 with Microsoft Fakes
我正在尝试将下面的 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都一样吗?
您的新测试似乎缺少将 keyValuePairs
数组的内容复制到 nameValueCollection
集合中的步骤。我相信这是在原始测试中通过以下行完成的:
nameValueCollection = PexFactories.CreateNameValueCollection(keyValuePairs);
我正在尝试将下面的 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都一样吗?
您的新测试似乎缺少将 keyValuePairs
数组的内容复制到 nameValueCollection
集合中的步骤。我相信这是在原始测试中通过以下行完成的:
nameValueCollection = PexFactories.CreateNameValueCollection(keyValuePairs);