Fix for problem faking two SPLists in a single unit test with Typemock Isolator has been released

A blogged a while ago about a problem faking multiple SPList with Typemock Isolator in a single test. With the release of Typemock Isolator 7.0.4.0 you no longer have to use the workaround I documented.

You can now use the code if the originally planned, and it works as expected

1 1: public partial class TestPage : System.Web.UI.Page 2:  { 3:     public TestPage() 4:     { 5:  var fakeWeb = Isolate.Fake.Instance<SPWeb>();  
2 7:        Isolate.WhenCalled(() => SPControl.GetContextWeb(null)).WillReturn(fakeWeb); 8:   9:        // return value for 1st call  
3 10:        Isolate.WhenCalled(() => fakeWeb.Lists\["Centre Locations"\].Items).WillReturnCollectionValuesOf(CreateCentreList()); 11:        // return value for all other calls 12:        Isolate.WhenCalled(() => fakeWeb.Lists\["Map Zoom Areas"\].Items).WillReturnCollectionValuesOf(CreateZoomAreaList()); 13:     } 14:  15:     private static List<SPListItem> CreateZoomAreaList()  
4 16:     { 17:        var fakeZoomAreas = new List<SPListItem>(); 18:        fakeZoomAreas.Add(CreateZoomAreaSPListItem("London", 51.49275, -0.137722222, 2, 14)); 19:        return fakeZoomAreas; 20:     } 21:  22:     private static List<SPListItem> CreateCentreList()  
5 23:     { 24:        var fakeSites = new List<SPListItem>(); 25:        fakeSites.Add(CreateCentreSPListItem("Aberdeen ", "1 The Road,  Aberdeen ", "Aberdeen@test.com", "www.Aberdeen.test.com", "1111", "2222", 57.13994444, -2.113333333)); 26:        fakeSites.Add(CreateCentreSPListItem("Altrincham ", "1 The Road,  Altrincham ", "Altrincham@test.com", "www.Altrincham.test.com", "3333", "4444", 53.38977778, -2.349916667)); 27:        return fakeSites; 28:     } 29:  30:     private static SPListItem CreateCentreSPListItem(string title, string address, string email, string url, string telephone, string fax, double lat, double lng)  
6 31:     { 32:         var fakeItem = Isolate.Fake.Instance<SPListItem>(); 33:         Isolate.WhenCalled(() => fakeItem\["Title"\]).WillReturn(title); 34:         Isolate.WhenCalled(() => fakeItem\["Address"\]).WillReturn(address); 35:         Isolate.WhenCalled(() => fakeItem\["Email Address"\]).WillReturn(email); 36:         Isolate.WhenCalled(() => fakeItem\["Site URL"\]).WillReturn(url); 37:         Isolate.WhenCalled(() => fakeItem\["Telephone"\]).WillReturn(telephone); 38:         Isolate.WhenCalled(() => fakeItem\["Fax"\]).WillReturn(fax); 39:         Isolate.WhenCalled(() => fakeItem\["Latitude"\]).WillReturn(lat.ToString()); 40:         Isolate.WhenCalled(() => fakeItem\["Longitude"\]).WillReturn(lng.ToString()); 41:         return fakeItem; 42:     } 43:  44:     private static SPListItem CreateZoomAreaSPListItem(string areaName, double lat, double lng, double radius, int zoom)  
7 45:     { 46:         var fakeItem = Isolate.Fake.Instance<SPListItem>(); 47:         Isolate.WhenCalled(() => fakeItem\["Title"\]).WillReturn(areaName); 48:         Isolate.WhenCalled(() => fakeItem\["Latitude"\]).WillReturn(lat.ToString()); 49:         Isolate.WhenCalled(() => fakeItem\["Longitude"\]).WillReturn(lng.ToString()); 50:         Isolate.WhenCalled(() => fakeItem\["Radius"\]).WillReturn(radius.ToString()); 51:         Isolate.WhenCalled(() => fakeItem\["Zoom"\]).WillReturn(zoom.ToString()); 52:         return fakeItem; 53:     } 54:  55: }

A check of the returned values shows

  • web.Lists["Centre Locations"].Items.Count returns 2
  • web.Lists["Map Zoom Areas"].Items.Count) returns 1