Hi,
We've found a workaround to this limitation:
The workaround is based on usage of SwapAll(), and map the behavior according to the instance being called at runtime. I'll try to show it using an example, where we fake all instances of SPSite and make their Url be based on the Guid used in their construction. It's a meaningless code in real life, but it should help understanding the concept.
[TestMethod, Isolated]
public void FakeBasedOnConcsturctorArgumentsExample()
{
// A fake instance we'll use to intercept all SPSite instances in the code
var fakeSPSite = Isolate.Fake.Instance<SPSite>();
// A map between an instance of SPSite and the faked Url value
var fakeUrlsMap = new Dictionary<SPSite>();
// Set the behavior to return for each SPSite the faked url from map
Isolate.WhenCalled(() => fakeSPSite.Url).DoInstead(ctx => fakeUrlsMap[(SPSite)ctx.Instance]);
// Gets the fake controller to acheive constructor interception
var fakeController = MockManager.GetMockOf(fakeSPSite);
// Place a callback for every SPSite construvtor
fakeController.MethodSettings(".ctor").MockMethodCalled += (sender, eventArgs) =>
{
// We assume in this example that only SPSites with GUID will be called
// It is simple to adjust it to any overload by querying the SentArguments
var guidUsedInConstructor = (Guid)eventArgs.SentArguments[0];
// The sender is the actual instance of SPSite the code under test holds
var instantiatedSite = (SPSite)sender;
// We're setting the fake behavior based on the constructor arguments
fakeUrlsMap[instantiatedSite] = guidUsedInConstructor + "Fake";
};
// Signals that every constructed SPSite behavior sould be swapped with our fake
Isolate.Swap.AllInstances<SPSite>().With(fakeSPSite);
// Shows that the Url is faked according to the Guid used in the constructor
var firstGuid = Guid.NewGuid();
var firstSPSite = new SPSite(firstGuid);
Assert.AreEqual(firstGuid + "Fake", firstSPSite.Url);
// Shows that the Url is faked according to the Guid used in the constructor
var secondGuid = Guid.NewGuid();
var secondSPSite = new SPSite(secondGuid);
Assert.AreEqual(secondGuid + "Fake", secondSPSite.Url);
}
Let me know if it helps.
Regards,
Elisha,
Typemock Support