如何伪造SharePoint列表(我需要IEnumerable <microsoft.sharepoint.client.listitem>)?</microsoft.sharepoint.client.listitem>

时间:2015-02-18 13:52:13

标签: c# .net unit-testing sharepoint

我正在为应用程序编写单元测试,它从SharePoint获取数据。 Project具有IListItemCollectionProvider接口和IEnumerable<Microsoft.SharePoint.Client.ListItem> GetListByTitle方法。所以,我想要伪造它,我只需要创建自己的IEnumerable<ListItem>。不幸的是,它不是那么容易,因为ListItem不能手动创建。 我试图使用Microsoft.SharePoint.Emulators,但它没有帮助,因为 1)仿真器需要.Net 3.5,而不是项目的4.5工作(这http://www.codeproject.com/Articles/545058/Couldplusnotplusloadplusfileplusorplusassemblyplus不起作用)。 2)仿真器假设使用Microsoft.SharePoint.SPList / SPListItemCollection / SPListItem,我找不到任何方法将它转换为ListItem。

我正在寻找另一种方法来获取我的假数据ListItem。

以防代码使用仿真器:

          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);
        }

1 个答案:

答案 0 :(得分:1)

实际上,SharePoint.Emulators仅适用于.NET Framework 3.5。

如果您需要能够在使用.NET Framework 4.5或更高版本的测试项目中伪造SharePoint 2013对象,您可能会对第三方NuGet包SPEmulators感兴趣。该文档可用here。此程序包的限制是它需要.NET Framework 4.5,并且不适用于.NET Framework 4.0。

对于那些需要.NET Framework 4.0解决方案的人来说,Sharepoint.StackExchange上的相关问题有an answer,其中包括在安装Microsoft <appsettings><add key="UserName" Value="abc" /></appsettings> <connectionstring><add name="Conn" connectionString="Server=test; Database=test; Uid=UserName; Pwd=test123;" /> 包期间更改.NET Framework的版本。正如here所述:

  

在3.5中安装Sharepoint.Emulator后,我再次将框架更改为4.0,项目仍然有效。