如何单元测试方法与Sharepoint对象作为参数与微软假

本文关键字:对象 参数 微软 Sharepoint 单元 测试方法 何单元 | 更新日期: 2023-09-27 18:05:38

我试图写一个单元测试的方法,以sp对象为参数,如SPWeb或SPEventReceiverCollection等与Microsoft Fakes,但不知道如何模拟这些。我以前用过moq,但似乎找不到如何Mock在微软假。如:

public List<SPEventReceiverDefinition> CheckExisting(SPEventReceiverDefinitionCollection recs)

如何单元测试方法与Sharepoint对象作为参数与微软假

            Microsoft.SharePoint.Fakes.ShimSPEventReceiverDefinition fakeERD = new Microsoft.SharePoint.Fakes.ShimSPEventReceiverDefinition();
        fakeERD.TypeGet = () => { return type; };
        fakeERD.AssemblyGet = () => { return assembly; };
        fakeERD.ClassGet = () => { return Constants.UsernameWriterPath; };

明白了。使用Fake类的区域也需要在ShimContext中。上面的例子展示了如何创建和设置对象/属性。

            using (ShimsContext.Create())
        {//testing code goes here}