I found the following post which describes how to test a custom model binder:
https://stackoverflow.com/a/55387164/155899
However, this tests an individual model binder. Ideally I'd like to be able to register all of my binders and test that the appropriate model was bound. This is how I would've achieved what I'm looking for in ASP.NET MVC:
// Register the model binders
ModelBinders.Binders[typeof(DateTime)] = new DateTimeModelBinder();
...
var values = new NameValueCollection {
{ "Foo", "1964/12/02 12:00:00" }
};
var controllerContext = CreateControllerContext(); // Utility method
var bindingContext = new ModelBindingContext() {
ModelName = "Foo",
ValueProvider = new NameValueCollectionValueProvider(values, null)
};
var binder = ModelBinders.Binders.GetBinder(typeof(DateTime));
var result = (DateTime)binder.BindModel(controllerContext, bindingContext);
Not only does this allow me to test the result of my model binder but it also makes sure the correct model binder is selected.
I'd appreciate it if someone could help. Thanks