Mocking Sitecore with Microsoft Fakes part 2
In this post I will continue where part 1 left, you can find part 1 here. This post will be rich with examples on how to use Microsoft fakes, now included in VS 2012 Premium, to mock out different parts of Sitecore. All the Examples has been chosen, because I think they include some of the most used functions or interactions with Sitecore. They are all really small example, and you can off course combine them as you like.
To start with lets take a look at the Sitecore Item and some of the properties not covered in part 1.
[TestMethod] public void Mocking_ItemProperties() { using (ShimsContext.Create()) { ID expectedId = ID.NewID; ID expectedTemplateId = ID.NewID; string expectedName = "ShimItem"; string expectedDisplayName = "ShimItem"; //Setting some of the most used properties on a Item Item item = new ShimItem() { IDGet = () => expectedId, NameGet = () => expectedName, DisplayNameGet = () => expectedDisplayName, TemplateIDGet = () => expectedTemplateId }; Assert.AreSame(expectedId, item.ID); Assert.AreSame(expectedTemplateId, item.TemplateID); Assert.AreSame(expectedName, item.Name); Assert.AreSame(expectedDisplayName, item.DisplayName); } } [TestMethod] public void Mocking_Item_Children() { using (ShimsContext.Create()) { //Setup an valid item list you can add more item properties if you like List<Item> childList = new List<Item>(); childList.Add(new ShimItem() { NameGet = () => "1" }); childList.Add(new ShimItem() { NameGet = () => "2" }); childList.Add(new ShimItem() { NameGet = () => "3" }); childList.Add(new ShimItem() { NameGet = () => "4" }); childList.Add(new ShimItem() { NameGet = () => "5" }); ChildList children = new ShimChildList() { GetEnumerator = () => childList.GetEnumerator(), CountGet = () => childList.Count }; Item item = new ShimItem() { ChildrenGet = () => children }; Assert.AreEqual(item.Children.Count,childList.Count); foreach (Item child in item.Children) { Assert.IsFalse(string.IsNullOrEmpty(child.Name)); } } }
With that covered lets see some examples of how to mock the Sitecore Database.
[TestMethod] public void Mocking_Database_GetName() { using (ShimsContext.Create()) { string expectedName = "web"; Database database = new ShimDatabase() { NameGet = () => expectedName }; Assert.AreEqual(expectedName, database.Name); } } [TestMethod] public void Mocking_Database_GetRootITem() { using (ShimsContext.Create()) { string expectedItemName = "Shim"; Item item = new ShimItem() { NameGet = () => expectedItemName }; Database database = new ShimDatabase() { GetRootItem = () => item }; Item rootItem = database.GetRootItem(); Assert.AreEqual(expectedItemName, rootItem.Name); } }
Finally lets combine the two above and try to mock the Static context of Sitecore. How often have you wrote Sitecore.Context.Item, Sitecore.Context.Site or Sitecore.Context.Database.
[TestMethod] public void Mocking_Context_Item() { using (ShimsContext.Create()) { string expectedName = "CurrentItem"; Item item = new ShimItem() { NameGet =()=> expectedName }; ShimContext.ItemGet = () => item; Item contextItem = Sitecore.Context.Item; Assert.AreEqual(expectedName, contextItem.Name); } } [TestMethod] public void Mocking_Context_Database() { using (ShimsContext.Create()) { string expectedDatabaseName = "web"; Database database = new ShimDatabase() { NameGet = () => expectedDatabaseName }; ShimContext.DatabaseGet = () => database; Database currentDatabase = Sitecore.Context.Database; Assert.AreEqual(expectedDatabaseName, currentDatabase.Name); } } [TestMethod] public void Mocking_Context_Site() { using (ShimsContext.Create()) { string expectedName = "CurrentItem"; Item item = new ShimItem() { NameGet =()=> expectedName, }; string expectedDatabaseName = "web"; Database database = new ShimDatabase() { NameGet = () => expectedDatabaseName }; SiteContext siteContext = new ShimSiteContext() { StartItemGet = () => item.Name, DatabaseGet = () => database }; ShimContext.SiteGet = () => siteContext; SiteContext context = Sitecore.Context.Site; Assert.AreEqual(expectedDatabaseName, context.Database.Name); Assert.AreEqual(expectedName, context.StartItem); } }
With that i think we pretty much covered some of the most used properties and method from Sitecore, without creating wrappers. If there are some missed examples of examples you would like to see please say so.
Otherwise stay tuned for Part 3.
Where is the code for ShimItem, ShimContext? Could you please provide the full solution?
I’m totally off you wrote about sitecore.fakes which you can read about here Sitecore Fakes getting started the test you wrote about is found on the github source for sitecore fakes. But now you are writing about ShimContext which i related to microsoft fakes and NOT mirosoft Fakes.
So to use Sitecore Fakes please read the link above to get you started with fakes. If you want to use ShimContext you shoulg not run the test you initially write about it demands for some other setup.
What is the “other setup”? Can You provide a full working example?
Microsoft fakes link Part 1 which is the first of three. Please do not I havent done one with microsoft fakes and security disabler / editcontext.