Search code examples
dynamicmoqorchardcms

Mocking Orchard dynamic types with Moq


I'm wondering whether anyone has any advice about effective ways to mock some of the Clay functionality in Orchard? I've gone as far as creating a wrapper interface, IClayWrapper, which I wrap around some of the more convoluted dynamic calls. Unfortunately, these wrapper methods need to return dynamic objects, so I'm still having problems. Here's a small example:

public interface IClayWrapper : IDependency
{
    dynamic BuildClipCreateViewModel(string projectTitle, string moduleTitle, dynamic clipForm);
}

public class WhenViewingCreateClip : GivenAClipController
{
    private IEnumerable<ContentItem> queryResults;

    protected override ClipController Establish_context()
    {
        var queryResults = new List<ContentItem>
        {
            new ContentItem(),
            new ContentItem(),
        };
        ClayWrapperMock.Setup(x => x.BuildLookupListFromQuery(queryResults, "Create")).Returns(It.IsAny<dynamic>());
    }
}

The problem I'm currently facing is that Moq returns this error:

SetUp : Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : 'Moq.Language.Flow.ISetup<Aqovia.Module.MotionGallery.Infrastructure.IClayWrapper,object>' does not contain a definition for 'Returns'

Basically, it's changed the Returns method from Moq into a dynamic call, which is one of the main issues that I was facing with the Clay stuff before I tried wrapping it. I've also tried introducing a fake shape factory with similar results. Has anyone found an effective way to test this stuff? Other than don't test it?


Solution

  • What's the point? Mocking a dynamic object is absurd, as you can build them much more easily than a mock, which is why there are typically no mocking frameworks in dynamic languages: you don't need them. Just use a real Clay object.

    You may also want to know that Clay will not ship with Orchard 1.7.