Testing with IShapeFactory

Topics: Writing modules
Oct 14, 2011 at 9:27 PM

Hi Everyone,

Has anyone figured out a good way to unit test a controller which takes in an IShapeFactory? I didn't see an example of this in any of the included tests but maybe I missed it. I have a controller which takes an IShapeFactory and an action which uses it to create a dynamic view model to pass to a view. Standard stuff in Orchard which looks basically like this:

public class FooController : Controller {

    public FooController(IShapeFactory shapeFactory) {
        Shape = shapeFactory;
    }

    private dynamic Shape { get; set; }
         
    public ActionResult Index(string bar) {
        var viewModel = Shape.ViewModel();
        viewModel.Bar(bar);

        return View(viewModel);
    }
}

I'd like to write a test and assert against the model on the ViewResult that is returned. However, I discovered that instantiating the DefaultShapeFactory is no easy feat due to the chain of dependencies. Does anybody have a good strategy for that?

Testing in general could probably use some attention. Looking through the tests included with the framework, there's a large amount of effort being spent on setting up the container, and it's repeated in a lot of places. Maybe an automocking container can help there somewhere?

Developer
Dec 9, 2011 at 7:53 PM
Edited Dec 9, 2011 at 7:54 PM

Although the question is quite old, this could maybe be helpful anyways:

The moq framework is included, so you could just mock an IShapeFactory instance as new Mock<IShapeFactory>.Object (although such a simple mock will most possible not cut it). Then you can register this instance with Autofac. Have you looked at the Orchard.Tests.Modules project? There are examples how to use mock objects together with Autofac. You really don't have to instantiate and inject anything by hand.

Dec 9, 2011 at 9:37 PM

Yeah, I know how to use Moq, but it's been a while since I posted this and I don't remember exactly what the problem was anymore. :-)  I think the issue was that I couldn't easily setup Moq to return a Shape, but I don't remember for sure.

Jan 16, 2012 at 10:38 AM
Edited Jan 16, 2012 at 10:39 AM

Sorry to dig up an old thread, but the info above is revleant to my question.

I'm learning TDD and orchard so please bear with me.

I've written a unit test for essentially the same code as above. I've set-up the IshapeFactory using autofac, but I'm unsure how I set the moq and viewmodel in for shapefactory.

 

   public void MyTestInitialize() {
             var builder = new ContainerBuilder();

             builder.RegisterType<ContentPartDriverCoordinator>().As<IContentHandler>();
             builder.RegisterType<DefaultShapeFactory>().As<IShapeFactory>();
             builder.RegisterInstance(new Mock<IContentDefinitionManager>().Object);
             _container = builder.Build();
         }

 

 

 

      public void Index_returns_no_menu_item()
        {
            //Arrange
            var contentManager = new Mock<Orchard.ContentManagement.IContentManager>();
            IShapeFactory shapeFactory = _container.Resolve<IShapeFactory>();

            // set viewmodel

            AdminController admincontroller = new AdminController(contentManager.Object, shapeFactory);

            //Act
         var actual = admincontroller.Items();

            //Assert
           actual.ShouldNotBeNull();
        }

 

How do I moq the viewmodel?

Jan 16, 2012 at 11:18 AM

Moq what viewmodel?

The normal way to create a view model with an IShapeFactory is:

dynamic factory = shapeFactory;

factory.ViewModel().Foo("Bar") ...

Jan 16, 2012 at 1:11 PM

Pete is right. You wouldn't want to mock a view model. A view model is usually just a simple class with some properties for passing data to the view. There is no behavior on it that you would want to mock. The complexity when using Orchard is that sometimes (not always) your view model is a dynamic created via the ShapeFactory, which is where my initial question arose. If you're using Orchard in a more classic MVC style you sometimes won't use the ShapeFactory anyway, which makes this thread moot because you can just instantiate a normal view model class in your test.