Object Creation Error
- Hi,I have a problem with Pex's object creation. I am converting the test cases in Microsoft Enterprise Cache Application Block to Parameterized unit tests in order to run Pex to test the caching app block. Specifically, I was trying to convert tests in ExpirationTaskFiture to PUTs.Here's the actual test:namespace Microsoft.Practices.EnterpriseLibrary.Caching.Tests{[TestClass]public class ExpirationTaskFixture : ICacheOperations{static Hashtable inMemoryCache;static ExpirationTask expirer;static string expiredItemKeys = "";public static int callbackCount;public static CacheItemRemovedReason callbackReason;CachingInstrumentationProvider instrumentationProvider;[TestInitialize]public void TestInitialize(){inMemoryCache = new Hashtable();instrumentationProvider = new CachingInstrumentationProvider();expirer = new ExpirationTask(this, instrumentationProvider);expiredItemKeys = "";callbackCount = 0;callbackReason = CacheItemRemovedReason.Unknown;}[TestMethod]public void NoExpirationsFoundIfNoItemsAreToBeExpired(){CacheItem neverExpiredItem = CreateNeverExpiredCacheItem("key", "value");AddCacheItem("key", neverExpiredItem);int expiredCount = expirer.MarkAsExpired(inMemoryCache);Assert.AreEqual(0, expiredCount, "Nothing in cache to expire");}void AddCacheItem(string key,CacheItem itemToAdd){inMemoryCache.Add(key, itemToAdd);}}}I converted the above test to the following:namespace PexTests{//Cannot generalize CacheItem since the object expiration policies//for each test case differs. Hence, the generalization is based on//taking inputs for the key value pairs that will be put into the//cache with different expiration policies defined within the test[TestClass][PexClass(typeof(Microsoft.Practices.EnterpriseLibrary.Caching.ExpirationTask))]public partial class ExpirationTaskFixturePUT : ICacheOperations{static Hashtable inMemoryCache;static ExpirationTask expirer;static string expiredItemKeys = "";public static int callbackCount;public static CacheItemRemovedReason callbackReason;CachingInstrumentationProvider instrumentationProvider;[TestInitialize]public void TestInitialize(){inMemoryCache = new Hashtable();instrumentationProvider = new CachingInstrumentationProvider();expirer = new ExpirationTask(this, instrumentationProvider);expiredItemKeys = "";callbackCount = 0;callbackReason = CacheItemRemovedReason.Unknown;}[PexMethod]public void NoExpirationsFoundIfNoItemsAreToBeExpired([PexAssumeUnderTest]string key, object obj){PexAssume.IsNotNull(obj);PexAssume.IsNotNullOrEmpty(key);PexAssume.IsFalse(key == "\0");CacheItem neverExpiredItem = new CacheItem(key, obj, CacheItemPriority.Normal, null, new NeverExpired());//CreateNeverExpiredCacheItem(key, obj);//AddCacheItem(key, obj);inMemoryCache.Add(key, neverExpiredItem);int expiredCount = expirer.MarkAsExpired(inMemoryCache);PexAssert.AreEqual(0, expiredCount, "Nothing in cache to expire");}void AddCacheItem(string key,object obj){CacheItem neverExpiredItem = new CacheItem(key, obj, CacheItemPriority.Normal, null, new NeverExpired());inMemoryCache.Add(key, neverExpiredItem);}}}The Problem: When I run Pex on this testcase, Pex generates an Object Creation failed error. The error states that Pex could not guess how to create CacheItem.This confuses me since I am not asking Pex to generate CacheItem at all. I am only taking the key and object as inputs from Pex. Why should Pex complain that CacheItem object creation failed when I am not asking it to?More background research:The error is most definitely in AddCacheItem(). When I comment out the line, Pex runs with no errors at all. Surprisingly, when I try to run Pex on the original test case itself, I again get the object creation failed error.Could someone please let me know the reason for this behaviour?Thanks,
Swaroop
All Replies
- > Pex generates an Object Creation failed error.
This is not an "error", but merely an "issue". Sometimes it's okay to ignore issues, and we added ways to ignore most issues. However, there is no way yet to ignore or suppress "Object Creation" issues.
In any case, I wonder: Besides reporting the issue, did Pex generate the test cases you expected?
> This confuses me since I am not asking Pex to generate CacheItem at all.
Not directly, but possibly indirectly.
True, your test parameter "obj" only has the formal type "object", so at first it seems that there is no need for Pex to create objects of any other, more complicated type.
However, once you bring the CacheItem into the picture, I strongly suspect that somewhere in the code invoked by your parameterized unit test, the value coming in from the "obj" parameter is related to "CacheItem". Does MarkAsExpect compare values? Maybe indirectly, via another dictionary or hashtable?
Internally, we have more information available when an "Object Creation" issue is raised: Pex thinks that it can "flip" a particular program branch if Pex could use another object of type CacheItem as test input. We could somehow show this information in the user interface. I guess that would have greatly helped you here?
Nikolai Tillmann - Tell us how you use Pex


