public async Task GetActionArguments_AddsActionArgumentsToModelStateDictionary_IfBinderReturnsTrue() { // Arrange Func<object, int> method = x => 1; var actionDescriptor = new ReflectedActionDescriptor { MethodInfo = method.Method, Parameters = new List<ParameterDescriptor> { new ParameterDescriptor { Name = "foo", ParameterBindingInfo = new ParameterBindingInfo("foo", typeof(object)) } } }; var value = "Hello world"; var binder = new Mock<IModelBinder>(); var metadataProvider = new EmptyModelMetadataProvider(); binder.Setup(b => b.BindModelAsync(It.IsAny<ModelBindingContext>())) .Callback((ModelBindingContext context) => { context.ModelMetadata = metadataProvider.GetMetadataForType(modelAccessor: null, modelType: typeof(string)); context.Model = value; }) .Returns(Task.FromResult(result: true)); var actionContext = new ActionContext(new RouteContext(Mock.Of<HttpContext>()), actionDescriptor); var bindingContext = new ActionBindingContext(actionContext, Mock.Of<IModelMetadataProvider>(), binder.Object, Mock.Of<IValueProvider>(), Mock.Of<IInputFormatterProvider>(), Enumerable.Empty<IModelValidatorProvider>()); var actionBindingContextProvider = new Mock<IActionBindingContextProvider>(); actionBindingContextProvider.Setup(p => p.GetActionBindingContextAsync(It.IsAny<ActionContext>())) .Returns(Task.FromResult(bindingContext)); var invoker = new ReflectedActionInvoker(actionContext, actionDescriptor, Mock.Of<IControllerFactory>(), actionBindingContextProvider.Object, Mock.Of<INestedProviderManager<FilterProviderContext>>()); var modelStateDictionary = new ModelStateDictionary(); // Act var result = await invoker.GetActionArguments(modelStateDictionary); // Assert Assert.Equal(1, result.Count); Assert.Equal(value, result["foo"]); }
public async Task GetActionArguments_DoesNotAddActionArgumentsToModelStateDictionary_IfBinderReturnsFalse() { // Arrange Func<object, int> method = x => 1; var actionDescriptor = new ReflectedActionDescriptor { MethodInfo = method.Method, Parameters = new List<ParameterDescriptor> { new ParameterDescriptor { Name = "foo", ParameterBindingInfo = new ParameterBindingInfo("foo", typeof(object)) } } }; var binder = new Mock<IModelBinder>(); binder.Setup(b => b.BindModelAsync(It.IsAny<ModelBindingContext>())) .Returns(Task.FromResult(result: false)); var actionContext = new ActionContext(new RouteContext(Mock.Of<HttpContext>()), actionDescriptor); var bindingContext = new ActionBindingContext(actionContext, Mock.Of<IModelMetadataProvider>(), binder.Object, Mock.Of<IValueProvider>(), Mock.Of<IInputFormatterProvider>(), Enumerable.Empty<IModelValidatorProvider>()); var actionBindingContextProvider = new Mock<IActionBindingContextProvider>(); actionBindingContextProvider.Setup(p => p.GetActionBindingContextAsync(It.IsAny<ActionContext>())) .Returns(Task.FromResult(bindingContext)); var invoker = new ReflectedActionInvoker(actionContext, actionDescriptor, Mock.Of<IControllerFactory>(), actionBindingContextProvider.Object, Mock.Of<INestedProviderManager<FilterProviderContext>>()); var modelStateDictionary = new ModelStateDictionary(); // Act var result = await invoker.GetActionArguments(modelStateDictionary); // Assert Assert.Empty(result); }