Stubbing the Orchard content manager

I’ve shown in the previous post how to build fake content items for testing purposes. When the code being tested gets content items from the content manager, however, you will also need a stub for the content manager, so your code receives fake content items that you have prepared, and not real ones from the database.

Such a stub content manager can be found here.

Not a lot is implemented on that stub, so you’ll need to add to the code as needed. In its present form, you can instantiate it, passing a list of fake content items as a constructor parameter, and then Get and GetMany will work against that set of items instead of the database.

var contentManager = new ContentManagerStub(fakeItems);

You can then inject that fake content manager into the constructor for the classes you are testing in place of the regular one.

Another possible usage of that fake content manager is to mock GetItemMetadata by adding an implementation to our stub:

public ContentItemMetadata GetItemMetadata(IContent contentItem) {
var titlePart = contentItem.As<TitlePart>();
var title = titlePart != null ? titlePart.Title : "";
return new ContentItemMetadata {
DisplayText = title,
DisplayRouteValues = new RouteValueDictionary {
{"controller", "content"},
{"action", "display"},
{"id", contentItem.Id}

This implementation gets the display text from the title part, and uses static route values. This can of course be modified as needed in your particular situation.

In the next post, I’ll show how to add fields to fake content items.

No Comments