public async Task ShouldBindDateTimeAsModel(string?year, string?month, bool isValid, string expectedDateString) { var formCollection = new FormCollection(new Dictionary <string, StringValues>() { { "Month", month?.ToString() }, { "Year", year?.ToString() }, }); var binder = new DateMonthYearModelBinder(); var vp = new FormValueProvider(BindingSource.Form, formCollection, CultureInfo.CurrentCulture); var context = GetBindingContext(vp, typeof(string)); await binder.BindModelAsync(context); context.ModelState.IsValid.Should().Be(isValid); if (isValid) { var dateValueString = (string)context.Result.Model; dateValueString.Should().Be(expectedDateString); } else { context.ModelState.ErrorCount.Should().Be(1); } }
public async Task ShouldNotBindIncompleteValueCollectionForDateTime_MissingYear() { var formCollection = new FormCollection(new Dictionary <string, StringValues>() { { "Month", "12" }, }); var binder = new DateMonthYearModelBinder(); var vp = new FormValueProvider(BindingSource.Form, formCollection, CultureInfo.CurrentCulture); var context = GetBindingContext(vp, typeof(string)); await binder.BindModelAsync(context); context.Result.Model.Should().BeNull(); }