Recently, I tried to write a unit test for my new SharePoint project. The Project contains EventListener which checks a TaxonomyField in a special list, and fill it, if field is empty.
Code looks like this:
public class CategoryListener: SPItemEventReceiver { ... public override void ItemCheckedIn(SPItemEventProperties properties) { using (SPWeb web = properties.OpenWeb()) { SPList mediaLib = web.Lists[properties.ListId]; SPListItem checkInItem = mediaLib.Items[properties.ListItemId]; if (checkInItem[cfg.CategoryField.Id] != null) return; Term categoryTerm = TermManager.GetAssociatedTerm(checkInItem, cfg.CategoryField); this.EventFiringEnabled = false; cfg.CategoryField.SetFieldValue(checkInItem, categoryTerm); this.EventFiringEnabled = true; } } ... } public class Configuration { public Guid MediaLibraryId { get; set; } public TaxonomyField CategoryField { get; set; } public bool AutocreateCategory { get; set; } public static Configuration Load() { ... } }
And I want to write unit test with Microsoft.Moles and SharePoint.Behaviour.
I try:
namespace Test.FullLibName { [TestClass] public class CategoryListenerTests { [TestMethod] [HostType("Moles")] public void CategoryListener_ItemCheckIn_UpdateCategory() { var bSPList = new BSPList(); bSPList.ID = new Guid("98b23464-b026-4525-bd22-6b590bd3abfe"); bSPList.Title = "ListTitle"; var bSPItem = bSPList.Items.SetOne(); bSPItem.ID = 1; ??? categoryField = new ???(); MConfiguration.Load = delegate() { return new Configuration() { CategoryField = categoryField, MediaLibraryId = new Guid("c3948036-778c-4064-bd40-c029139efeca") }; }; CategoryListener listener = new CategoryListener(); ListLog log = new ListLog(); listener.Log = log; var fProperties = new BSPItemEventProperties(); fProperties.ListTitle = bSPList.Title; fProperties.ListId = bSPList.ID; fProperties.ListItemId = 1; listener.ItemCheckedIn(fProperties); /// Asserts } } }
How I can test it?