【问题标题】:Convert the Pex TestMethods of VS 2010 to the VS2013 with Microsoft Fakes使用 Microsoft Fakes 将 VS 2010 的 Pex TestMethods 转换为 VS2013
【发布时间】:2015-05-08 15:44:22
【问题描述】:

我正在尝试将以下 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 都相同吗?

【问题讨论】:

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


    【解决方案1】:

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

    nameValueCollection = PexFactories.CreateNameValueCollection(keyValuePairs);
    

    【讨论】:

    • 我忘了在这篇文章中添加该步骤。除了这个还有其他意见吗?
    • 如果您进行了此修复,我看不到第一个测试中涵盖的第二个测试中未涵盖的任何场景。我在做一个假设,这条线:this.Initialize(errorLog, "", nameValueCollection); 在功能上等同于这条线:errorLog.Initialize("", nameValueCollection);
    • 是的,已经转换了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多