mercredi 18 février 2015

I'm writing Unit Tests for application, that gets data from SharePoint. Project has interface IListItemCollectionProvider with IEnumerable<Microsoft.SharePoint.Client.ListItem> GetListByTitle method. So, I want fake it and I just need to create my own IEnumerable<ListItem>. Unfortunately it's not so easy, because ListItem сan not be created manually. I tried to use Microsoft.SharePoint.Emulators, but it doesnt help, because 1)Emulators requires .Net 3.5, instead project's 4.5 to work (this http://ift.tt/XMmEvm doesn't work). 2)Emulators supposes using Microsoft.SharePoint.SPList/SPListItemCollection/SPListItem, and I can't find any way to transform it to ListItem.


I looking for another way get ListItem with my fake data.


Just in case code with Emulators:



using (var emulationScope = new SharePointEmulationScope(EmulationMode.Enabled))
//throws System.IO.FileNotFoundException: Could not load file or assembly 'System.Web.2.0.0.0.Fakes
{
SPSite site = new Microsoft.SharePoint.SPSite("http://localhost");
string listName = String.Format("List{0}", Guid.NewGuid());

// create a new temporary list
Guid listId = site.RootWeb.Lists.Add(listName, listName, SPListTemplateType.GenericList);
Microsoft.SharePoint.SPList list = site.RootWeb.Lists[listId];
Assert.IsNotNull(list, "Fail create fake SPList");

// add fields to list
list.Fields.Add("jobname", SPFieldType.Text, true);
list.Fields.Add("timestamp", SPFieldType.Text, true);
list.Update();


// insert 1 item into list
Microsoft.SharePoint.SPItem item = list.Items.Add();
item["timestamp"] = "fake-guid";
item["jobname"] = "fake-job";
item.Update();

Microsoft.SharePoint.SPListItemCollection listItemCollection = list.Items;
var clientContext = new ClientContext(site.Url);
foreach (var elem in listItemCollection)//var is System.Object somehow
{
var tmp = elem.ToString();
tmp = "";
}

//need something like "insert 1 item into list" from above, but ListItem type
IEnumerable<ListItem> fakeTimeStampsList = null;

//using moq here
var mockDataProvider = new Mock<IListItemCollectionProvider>();
mockDataProvider.Setup(foo => foo.GetListByTitle(It.IsAny<string>(), null, null))
.Returns<IEnumerable<ListItem>>(_ => fakeTimeStampsList);

//how it should work:
//var result = mockDataProvider.Object.GetListByTitle("NoMetterWhatHere");,
//where reult is IEnumerable<ListItem> with 1 ListItem inside
//and it contains {[timestamp, fake-guid]}, {[jobname, fake-job]}

//Finally instance class (by fake IListItemCollectionProvider), that should be tested
var Page = new BaseReportPage(mockDataProvider.Object);
}

Aucun commentaire:

Enregistrer un commentaire