I have written custom model binder for a property. Now I am trying to write unit test for the same but not able to create object for model binder. Can anyone help me ? Below is the code for which I have to write test.
public class JourneyTypesModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
bool IsSingleWay = Convert.ToBoolean((bindingContext.ValueProvider.GetValue("IsSingleWay")).FirstValue);
bool IsMultiWay = Convert.ToBoolean((bindingContext.ValueProvider.GetValue("IsMultiWay")).FirstValue);
JourneyTypes journeyType = JourneyTypes.None;
bool hasJourneyType = Enum.TryParse((bindingContext.ValueProvider.GetValue("JourneyType")).FirstValue, out journeyType);
if (!hasJourneyType)
{
if (IsSingleWay)
journeyType = JourneyTypes.Single;
else journeyType = JourneyTypes.MultiWay;
}
bindingContext.Result = ModelBindingResult.Success(journeyType);
return Task.CompletedTask;
} }