public async Task BindModel_ReturnsNothing_IfNoBinderTypeIsSet() { // Arrange var bindingContext = GetBindingContext(typeof(Person)); var binder = new BinderTypeBasedModelBinder(); // Act var binderResult = await binder.BindModelResultAsync(bindingContext); // Assert Assert.Equal(default(ModelBindingResult), binderResult); }
public async Task BindModel_ReturnsFailedResult_EvenIfSelectedBinderReturnsNull() { // Arrange var bindingContext = GetBindingContext(typeof(Person), binderType: typeof(NullModelBinder)); var binder = new BinderTypeBasedModelBinder(); // Act var binderResult = await binder.BindModelResultAsync(bindingContext); // Assert Assert.NotEqual(default(ModelBindingResult), binderResult); Assert.False(binderResult.IsModelSet); }
public async Task BindModel_ForNonModelBinder_Throws() { // Arrange var bindingContext = GetBindingContext(typeof(Person), binderType: typeof(Person)); var binder = new BinderTypeBasedModelBinder(); var expected = $"The type '{typeof(Person).FullName}' must implement " + $"'{typeof(IModelBinder).FullName}' to be used as a model binder."; // Act var ex = await Assert.ThrowsAsync <InvalidOperationException>( () => binder.BindModelResultAsync(bindingContext)); // Assert Assert.Equal(expected, ex.Message); }
public async Task BindModel_CallsBindAsync_OnProvidedModelBinder() { // Arrange var bindingContext = GetBindingContext(typeof(Person), binderType: typeof(NotNullModelBinder)); var model = new Person(); var serviceProvider = new ServiceCollection() .AddSingleton <IModelBinder, NullModelBinder>() .BuildServiceProvider(); bindingContext.OperationBindingContext.HttpContext.RequestServices = serviceProvider; var binder = new BinderTypeBasedModelBinder(); // Act var binderResult = await binder.BindModelResultAsync(bindingContext); // Assert var p = (Person)binderResult.Model; Assert.Equal(model.Age, p.Age); Assert.Equal(model.Name, p.Name); }