I've been tasked with writing a small C# tool to fetch and fiddle with some Sharepoint 2010 items using the Sharepoint Client Object Model (COM). The code will typically have something like this in the beginning:
public void foo(ClientContext clientContext ) {
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
...
}
To unit test this, I was planning to use Microsoft's Mole. So I try to make a test with a mock ClientContext
like the following. The problem here is that I'm unable to make a mock implementation of the Load
method.
MClientContext ctx = new MClientContext {
WebGet = () => new MWeb {},
ExecuteQuery = () => {}
//,Load = () => {} .... problem
};
VS tells me there's no definition for Load
in Microsoft.SharePoint.Client.Moles.MClientContext
.
The ClientContext
's Load
method is inherited from ClientRuntimeContext
(I checked this with JustDecompile).
public void Load<T>(T clientObject, params Expression<Func<T, object>>[] retrievals)
where T : ClientObject
{
if (clientObject == null)
{
throw new ArgumentNullException("clientObject");
}
DataRetrieval.Load<T>(clientObject, retrievals);
}
So the question is, how does one create a mock ClientContext
with Mole, complete with the inherited Load
method, for unit testing? Is there some other trick to unit testing Client Object Model code?
Anything from example snippets to links would be greatly appreciated.
note: I'm very new to Sharepoint (and C#.... and, tbh, mocking). note2: I'm not able to get the paid TypeMock version, which I'm told could possibly accomplish this.
## Update: Solved! Essential clues provided by Nathan below ##
MClientContext ctx = new MClientContext {
WebGet = () => new MWeb {},
ExecuteQuery = () => {}
/* .... */
};
var ctxBase = new MClientRuntimeContext(ctx);
ctxBase.LoadTExpressionOfFuncOfTObjectArray<ListItemCollection>((a,b) => {});
This means my Moled ClientContext
can now do:
ctx.Load(someInstanceOfListItemCollection);
ctx.ExecuteQuery();