Search code examples
c#unit-testingmicrosoft-fakespex

Convert the Pex TestMethods of VS 2010 to the VS2013 with Microsoft Fakes


I am trying to convert the below pex testmethod to a normal unit test. Though I am planning to use Microsoft Fakes where it is required, I want to understand few things first.

[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);
}

And I have converted that to a simple unit test like below:

[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);
}

I have two questions here:

  • Do I loose any scenarios in this conversion from pexmethod to testmethod ?
  • I see the count in nameValueCollection. The returning count value is 1. Is that because all KeyValuePair I am inserting are the same?

Solution

  • Your new test appears to be missing the step where you copy the contents of the keyValuePairs array into the nameValueCollection collection. I believe this was done in the original test by this line:

    nameValueCollection = PexFactories.CreateNameValueCollection(keyValuePairs);