asp.net-mvc-3custom-model-binderwebactivator

Testing If a class is being activated using WebActivator and if add a IModelBinder to ModelBinderProviders.BinderProviders.


Dear fellows from Stack Exchange.

I'm trying to test if my Custom Model Binder is being added to the ModelBinderProviders.BinderProviders collection. I decided to activate this through WebActivator, to avoid messing global.asax, Everything works fine, but the Test:

I tried using the WebActivator.ActivationManager.Run() method, but my things weren't loaded. I've something like this in my test:

[TestMethod] 
public void TemplateModelBinderProvider_Should_Be_Registered_In_BinderProviders()
{
    WebActivator.ActivationManager.Run();
    IModelBinderProvider templateModelBinderProvider = ModelBinderProviders.BinderProviders.
        Where(x => x is TemplateModelBinderProvider).
        FirstOrDefault();

    Assert.IsNotNull(templateModelBinderProvider);
}

And this is my app_Start class:

[assembly: WebActivator.PreApplicationStartMethod(typeof(MVC.App_Start.MVCBindings), "Start")]
namespace MVC.App_Start
{
    public static class MVCBindings
    {       
        public static void Start()
        {
            ModelBinderProviders.BinderProviders.Add(new TemplateModelBinderProvider());
        }
    }
}

Solution

  • Sorry you have problems with the piece of code I wrote.

    I don't have access to the source code right now but will take a look in the evening (UK time).

    Do you think you could send me your solution so I could replicate it locally? My email is jkonecki at gmail.com

    UPDATE

    I have received your source code but unfortunately it contains references to libraries I cannot obtain so I cannot compile it.

    I have created a separate solution (emailed to you) with MVC3 web app and unit test projects that uses your custom model binder provide. There are two tests that prove that WebActivatorManager.Run method properly registers a custom provider.

    Try debugging your unit test to make sure that Run method calls your static Start method.

    WebActivator source code is here - you might want to get it and step through.