689 lines
26 KiB
C#
689 lines
26 KiB
C#
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using Microsoft.AspNet.Mvc.ModelBinding;
|
|
using Microsoft.AspNet.Routing;
|
|
using Microsoft.AspNet.Testing;
|
|
#if NET45
|
|
using Moq;
|
|
#endif
|
|
using Xunit;
|
|
using Microsoft.AspNet.Http;
|
|
|
|
namespace Microsoft.AspNet.Mvc.Test
|
|
{
|
|
public class ControllerTests
|
|
{
|
|
public static IEnumerable<object[]> PublicNormalMethodsFromController
|
|
{
|
|
get
|
|
{
|
|
return typeof(Controller).GetTypeInfo()
|
|
.DeclaredMethods
|
|
.Where(method => method.IsPublic && !method.IsSpecialName)
|
|
.Select(method => new[] { method });
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void SettingViewData_AlsoUpdatesViewBag()
|
|
{
|
|
// Arrange
|
|
var metadataProvider = new DataAnnotationsModelMetadataProvider();
|
|
var controller = new Controller();
|
|
var originalViewData = controller.ViewData = new ViewDataDictionary<object>(metadataProvider);
|
|
var replacementViewData = new ViewDataDictionary<object>(metadataProvider);
|
|
|
|
// Act
|
|
controller.ViewBag.Hello = "goodbye";
|
|
controller.ViewData = replacementViewData;
|
|
controller.ViewBag.Another = "property";
|
|
|
|
// Assert
|
|
Assert.NotSame(originalViewData, controller.ViewData);
|
|
Assert.Same(replacementViewData, controller.ViewData);
|
|
Assert.Null(controller.ViewBag.Hello);
|
|
Assert.Equal("property", controller.ViewBag.Another);
|
|
Assert.Equal("property", controller.ViewData["Another"]);
|
|
}
|
|
|
|
[Fact]
|
|
public void Redirect_WithParameterUrl_SetsRedirectResultSameUrl()
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
var url = "/test/url";
|
|
|
|
// Act
|
|
var result = controller.Redirect(url);
|
|
|
|
// Assert
|
|
Assert.IsType<RedirectResult>(result);
|
|
Assert.False(result.Permanent);
|
|
Assert.Same(url, result.Url);
|
|
}
|
|
|
|
[Fact]
|
|
public void RedirectPermanent_WithParameterUrl_SetsRedirectResultPermanentAndSameUrl()
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
var url = "/test/url";
|
|
|
|
// Act
|
|
var result = controller.RedirectPermanent(url);
|
|
|
|
// Assert
|
|
Assert.IsType<RedirectResult>(result);
|
|
Assert.True(result.Permanent);
|
|
Assert.Same(url, result.Url);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(null)]
|
|
[InlineData("")]
|
|
public void Redirect_WithParameter_NullOrEmptyUrl_Throws(string url)
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
|
|
// Act & Assert
|
|
ExceptionAssert.ThrowsArgument(
|
|
() => controller.Redirect(url: url), "url", "The value cannot be null or empty");
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(null)]
|
|
[InlineData("")]
|
|
public void RedirectPermanent_WithParameter_NullOrEmptyUrl_Throws(string url)
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
|
|
// Act & Assert
|
|
ExceptionAssert.ThrowsArgument(
|
|
() => controller.RedirectPermanent(url: url), "url", "The value cannot be null or empty");
|
|
}
|
|
|
|
[Fact]
|
|
public void RedirectToAction_WithParameterActionName_SetsResultActionName()
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
|
|
// Act
|
|
var resultTemporary = controller.RedirectToAction("SampleAction");
|
|
|
|
// Assert
|
|
Assert.IsType<RedirectToActionResult>(resultTemporary);
|
|
Assert.False(resultTemporary.Permanent);
|
|
Assert.Equal("SampleAction", resultTemporary.ActionName);
|
|
}
|
|
|
|
[Fact]
|
|
public void RedirectToActionPermanent_WithParameterActionName_SetsResultActionNameAndPermanent()
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
|
|
// Act
|
|
var resultPermanent = controller.RedirectToActionPermanent("SampleAction");
|
|
|
|
// Assert
|
|
Assert.IsType<RedirectToActionResult>(resultPermanent);
|
|
Assert.True(resultPermanent.Permanent);
|
|
Assert.Equal("SampleAction", resultPermanent.ActionName);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData(null)]
|
|
[InlineData("SampleController")]
|
|
public void RedirectToAction_WithParameterActionAndControllerName_SetsEqualNames(string controllerName)
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
|
|
// Act
|
|
var resultTemporary = controller.RedirectToAction("SampleAction", controllerName);
|
|
|
|
// Assert
|
|
Assert.IsType<RedirectToActionResult>(resultTemporary);
|
|
Assert.False(resultTemporary.Permanent);
|
|
Assert.Equal("SampleAction", resultTemporary.ActionName);
|
|
Assert.Equal(controllerName, resultTemporary.ControllerName);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData(null)]
|
|
[InlineData("SampleController")]
|
|
public void RedirectToActionPermanent_WithParameterActionAndControllerName_SetsEqualNames(
|
|
string controllerName)
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
|
|
// Act
|
|
var resultPermanent = controller.RedirectToActionPermanent("SampleAction", controllerName);
|
|
|
|
// Assert
|
|
Assert.IsType<RedirectToActionResult>(resultPermanent);
|
|
Assert.True(resultPermanent.Permanent);
|
|
Assert.Equal("SampleAction", resultPermanent.ActionName);
|
|
Assert.Equal(controllerName, resultPermanent.ControllerName);
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData("RedirectTestData")]
|
|
public void RedirectToAction_WithParameterActionControllerRouteValues_SetsResultProperties(object routeValues)
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
|
|
// Act
|
|
var resultTemporary = controller.RedirectToAction("SampleAction", "SampleController", routeValues);
|
|
|
|
// Assert
|
|
Assert.IsType<RedirectToActionResult>(resultTemporary);
|
|
Assert.False(resultTemporary.Permanent);
|
|
Assert.Equal("SampleAction", resultTemporary.ActionName);
|
|
Assert.Equal("SampleController", resultTemporary.ControllerName);
|
|
Assert.Equal(TypeHelper.ObjectToDictionary(routeValues), resultTemporary.RouteValues);
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData("RedirectTestData")]
|
|
public void RedirectToActionPermanent_WithParameterActionControllerRouteValues_SetsResultProperties(
|
|
object routeValues)
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
|
|
// Act
|
|
var resultPermanent = controller.RedirectToActionPermanent(
|
|
"SampleAction",
|
|
"SampleController",
|
|
routeValues);
|
|
|
|
// Assert
|
|
Assert.IsType<RedirectToActionResult>(resultPermanent);
|
|
Assert.True(resultPermanent.Permanent);
|
|
Assert.Equal("SampleAction", resultPermanent.ActionName);
|
|
Assert.Equal("SampleController", resultPermanent.ControllerName);
|
|
Assert.Equal(TypeHelper.ObjectToDictionary(routeValues), resultPermanent.RouteValues);
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData("RedirectTestData")]
|
|
public void RedirectToAction_WithParameterActionAndRouteValues_SetsResultProperties(object routeValues)
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
|
|
// Act
|
|
var resultTemporary = controller.RedirectToAction(actionName: null, routeValues: routeValues);
|
|
|
|
// Assert
|
|
Assert.IsType<RedirectToActionResult>(resultTemporary);
|
|
Assert.False(resultTemporary.Permanent);
|
|
Assert.Null(resultTemporary.ActionName);
|
|
Assert.Equal(TypeHelper.ObjectToDictionary(routeValues), resultTemporary.RouteValues);
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData("RedirectTestData")]
|
|
public void RedirectToActionPermanent_WithParameterActionAndRouteValues_SetsResultProperties(
|
|
object routeValues)
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
|
|
// Act
|
|
var resultPermanent = controller.RedirectToActionPermanent(null, routeValues);
|
|
|
|
// Assert
|
|
Assert.IsType<RedirectToActionResult>(resultPermanent);
|
|
Assert.True(resultPermanent.Permanent);
|
|
Assert.Null(resultPermanent.ActionName);
|
|
Assert.Equal(TypeHelper.ObjectToDictionary(routeValues), resultPermanent.RouteValues);
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData("RedirectTestData")]
|
|
public void RedirectToRoute_WithParameterRouteValues_SetsResultEqualRouteValues(object routeValues)
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
|
|
// Act
|
|
var resultTemporary = controller.RedirectToRoute(routeValues);
|
|
|
|
// Assert
|
|
Assert.IsType<RedirectToRouteResult>(resultTemporary);
|
|
Assert.False(resultTemporary.Permanent);
|
|
Assert.Equal(TypeHelper.ObjectToDictionary(routeValues), resultTemporary.RouteValues);
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData("RedirectTestData")]
|
|
public void RedirectToRoutePermanent_WithParameterRouteValues_SetsResultEqualRouteValuesAndPermanent(
|
|
object routeValues)
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
|
|
// Act
|
|
var resultPermanent = controller.RedirectToRoutePermanent(routeValues);
|
|
|
|
// Assert
|
|
Assert.IsType<RedirectToRouteResult>(resultPermanent);
|
|
Assert.True(resultPermanent.Permanent);
|
|
Assert.Equal(TypeHelper.ObjectToDictionary(routeValues), resultPermanent.RouteValues);
|
|
}
|
|
|
|
[Fact]
|
|
public void RedirectToRoute_WithParameterRouteName_SetsResultSameRouteName()
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
var routeName = "CustomRouteName";
|
|
|
|
// Act
|
|
var resultTemporary = controller.RedirectToRoute(routeName);
|
|
|
|
// Assert
|
|
Assert.IsType<RedirectToRouteResult>(resultTemporary);
|
|
Assert.False(resultTemporary.Permanent);
|
|
Assert.Same(routeName, resultTemporary.RouteName);
|
|
}
|
|
|
|
[Fact]
|
|
public void RedirectToRoutePermanent_WithParameterRouteName_SetsResultSameRouteNameAndPermanent()
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
var routeName = "CustomRouteName";
|
|
|
|
// Act
|
|
var resultPermanent = controller.RedirectToRoutePermanent(routeName);
|
|
|
|
// Assert
|
|
Assert.IsType<RedirectToRouteResult>(resultPermanent);
|
|
Assert.True(resultPermanent.Permanent);
|
|
Assert.Same(routeName, resultPermanent.RouteName);
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData("RedirectTestData")]
|
|
public void RedirectToRoute_WithParameterRouteNameAndRouteValues_SetsResultSameRouteNameAndRouteValues(
|
|
object routeValues)
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
var routeName = "CustomRouteName";
|
|
|
|
// Act
|
|
var resultTemporary = controller.RedirectToRoute(routeName, routeValues);
|
|
|
|
// Assert
|
|
Assert.IsType<RedirectToRouteResult>(resultTemporary);
|
|
Assert.False(resultTemporary.Permanent);
|
|
Assert.Same(routeName, resultTemporary.RouteName);
|
|
Assert.Equal(TypeHelper.ObjectToDictionary(routeValues), resultTemporary.RouteValues);
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData("RedirectTestData")]
|
|
public void RedirectToRoutePermanent_WithParameterRouteNameAndRouteValues_SetsResultProperties(
|
|
object routeValues)
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
var routeName = "CustomRouteName";
|
|
|
|
// Act
|
|
var resultPermanent = controller.RedirectToRoutePermanent(routeName, routeValues);
|
|
|
|
// Assert
|
|
Assert.IsType<RedirectToRouteResult>(resultPermanent);
|
|
Assert.True(resultPermanent.Permanent);
|
|
Assert.Same(routeName, resultPermanent.RouteName);
|
|
Assert.Equal(TypeHelper.ObjectToDictionary(routeValues), resultPermanent.RouteValues);
|
|
}
|
|
|
|
[Fact]
|
|
public void HttpNotFound_SetsStatusCode()
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
|
|
// Act
|
|
var result = controller.HttpNotFound();
|
|
|
|
// Assert
|
|
Assert.IsType<HttpNotFoundResult>(result);
|
|
Assert.Equal(404, result.StatusCode);
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData("PublicNormalMethodsFromController")]
|
|
public void NonActionAttribute_IsOnEveryPublicNormalMethodFromController(MethodInfo method)
|
|
{
|
|
// Arrange & Act & Assert
|
|
Assert.True(method.IsDefined(typeof(NonActionAttribute)));
|
|
}
|
|
|
|
[Fact]
|
|
public void Controller_View_WithoutParameter_SetsResultNullViewNameAndNullViewDataModel()
|
|
{
|
|
// Arrange
|
|
var controller = new Controller()
|
|
{
|
|
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
|
|
};
|
|
|
|
// Act
|
|
var actualViewResult = controller.View();
|
|
|
|
// Assert
|
|
Assert.IsType<ViewResult>(actualViewResult);
|
|
Assert.Null(actualViewResult.ViewName);
|
|
Assert.Same(controller.ViewData, actualViewResult.ViewData);
|
|
Assert.Null(actualViewResult.ViewData.Model);
|
|
}
|
|
|
|
[Fact]
|
|
public void Controller_View_WithParameterViewName_SetsResultViewNameAndNullViewDataModel()
|
|
{
|
|
// Arrange
|
|
var controller = new Controller()
|
|
{
|
|
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
|
|
};
|
|
|
|
// Act
|
|
var actualViewResult = controller.View("CustomViewName");
|
|
|
|
// Assert
|
|
Assert.IsType<ViewResult>(actualViewResult);
|
|
Assert.Equal("CustomViewName", actualViewResult.ViewName);
|
|
Assert.Same(controller.ViewData, actualViewResult.ViewData);
|
|
Assert.Null(actualViewResult.ViewData.Model);
|
|
}
|
|
|
|
[Fact]
|
|
public void Controller_View_WithParameterViewModel_SetsResultNullViewNameAndViewDataModel()
|
|
{
|
|
// Arrange
|
|
var controller = new Controller()
|
|
{
|
|
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
|
|
};
|
|
var model = new object();
|
|
|
|
// Act
|
|
var actualViewResult = controller.View(model);
|
|
|
|
// Assert
|
|
Assert.IsType<ViewResult>(actualViewResult);
|
|
Assert.Null(actualViewResult.ViewName);
|
|
Assert.Same(controller.ViewData, actualViewResult.ViewData);
|
|
Assert.Same(model, actualViewResult.ViewData.Model);
|
|
}
|
|
|
|
[Fact]
|
|
public void Controller_View_WithParameterViewNameAndViewModel_SetsResultViewNameAndViewDataModel()
|
|
{
|
|
// Arrange
|
|
var controller = new Controller()
|
|
{
|
|
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
|
|
};
|
|
var model = new object();
|
|
|
|
// Act
|
|
var actualViewResult = controller.View("CustomViewName", model);
|
|
|
|
// Assert
|
|
Assert.IsType<ViewResult>(actualViewResult);
|
|
Assert.Equal("CustomViewName", actualViewResult.ViewName);
|
|
Assert.Same(controller.ViewData, actualViewResult.ViewData);
|
|
Assert.Same(model, actualViewResult.ViewData.Model);
|
|
}
|
|
|
|
[Fact]
|
|
public void Controller_Content_WithParameterContentString_SetsResultContent()
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
|
|
// Act
|
|
var actualContentResult = controller.Content("TestContent");
|
|
|
|
// Assert
|
|
Assert.IsType<ContentResult>(actualContentResult);
|
|
Assert.Equal("TestContent", actualContentResult.Content);
|
|
Assert.Null(actualContentResult.ContentEncoding);
|
|
Assert.Null(actualContentResult.ContentType);
|
|
}
|
|
|
|
[Fact]
|
|
public void Controller_Content_WithParameterContentStringAndContentType_SetsResultContentAndContentType()
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
|
|
// Act
|
|
var actualContentResult = controller.Content("TestContent", "text/plain");
|
|
|
|
// Assert
|
|
Assert.IsType<ContentResult>(actualContentResult);
|
|
Assert.Equal("TestContent", actualContentResult.Content);
|
|
Assert.Null(actualContentResult.ContentEncoding);
|
|
Assert.Equal("text/plain", actualContentResult.ContentType);
|
|
}
|
|
|
|
[Fact]
|
|
public void Controller_Content_WithParameterContentAndTypeAndEncoding_SetsResultContentAndTypeAndEncoding()
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
|
|
// Act
|
|
var actualContentResult = controller.Content("TestContent", "text/plain", Encoding.UTF8);
|
|
|
|
// Assert
|
|
Assert.IsType<ContentResult>(actualContentResult);
|
|
Assert.Equal("TestContent", actualContentResult.Content);
|
|
Assert.Same(Encoding.UTF8, actualContentResult.ContentEncoding);
|
|
Assert.Equal("text/plain", actualContentResult.ContentType);
|
|
}
|
|
|
|
[Fact]
|
|
public void Controller_Json_WithParameterValue_SetsResultData()
|
|
{
|
|
// Arrange
|
|
var controller = new Controller();
|
|
var data = new object();
|
|
|
|
// Act
|
|
var actualJsonResult = controller.Json(data);
|
|
|
|
// Assert
|
|
Assert.IsType<JsonResult>(actualJsonResult);
|
|
Assert.Same(data, actualJsonResult.Data);
|
|
}
|
|
|
|
public static IEnumerable<object[]> RedirectTestData
|
|
{
|
|
get
|
|
{
|
|
yield return new object[] { null };
|
|
yield return new object[]
|
|
{
|
|
new Dictionary<string, string>() { { "hello", "world" } },
|
|
};
|
|
yield return new object[]
|
|
{
|
|
new RouteValueDictionary(new Dictionary<string, string>()
|
|
{
|
|
{ "test", "case" },
|
|
{ "sample", "route" },
|
|
}),
|
|
};
|
|
}
|
|
}
|
|
|
|
// These tests share code with the ActionFilterAttribute tests because the various filter
|
|
// implementations need to behave the same way.
|
|
#if NET45
|
|
[Fact]
|
|
public async Task Controller_ActionFilter_SettingResult_ShortCircuits()
|
|
{
|
|
await ActionFilterAttributeTests.ActionFilter_SettingResult_ShortCircuits(
|
|
new Mock<Controller>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Controller_ActionFilter_Calls_OnActionExecuted()
|
|
{
|
|
await ActionFilterAttributeTests.ActionFilter_Calls_OnActionExecuted(
|
|
new Mock<Controller>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TryUpdateModel_UsesModelTypeNameIfNotSpecified()
|
|
{
|
|
var metadataProvider = new DataAnnotationsModelMetadataProvider();
|
|
var valueProvider = Mock.Of<IValueProvider>();
|
|
var binder = new Mock<IModelBinder>();
|
|
binder.Setup(b => b.BindModelAsync(It.IsAny<ModelBindingContext>()))
|
|
.Callback((ModelBindingContext b) =>
|
|
{
|
|
Assert.Equal(typeof(MyModel).Name, b.ModelName);
|
|
Assert.Same(valueProvider, b.ValueProvider);
|
|
})
|
|
.Returns(Task.FromResult(false))
|
|
.Verifiable();
|
|
var model = new MyModel();
|
|
var actionContext = new ActionContext(Mock.Of<HttpContext>(), new RouteData(), new ActionDescriptor());
|
|
var bindingContext = new ActionBindingContext(actionContext,
|
|
metadataProvider,
|
|
binder.Object,
|
|
valueProvider,
|
|
Mock.Of<IInputFormatterProvider>(),
|
|
Enumerable.Empty<IModelValidatorProvider>());
|
|
var bindingContextProvider = new Mock<IActionBindingContextProvider>();
|
|
bindingContextProvider.Setup(b => b.GetActionBindingContextAsync(actionContext))
|
|
.Returns(Task.FromResult(bindingContext));
|
|
var viewData = new ViewDataDictionary(metadataProvider, new ModelStateDictionary());
|
|
var controller = new Controller
|
|
{
|
|
ActionContext = actionContext,
|
|
BindingContextProvider = bindingContextProvider.Object,
|
|
ViewData = viewData
|
|
};
|
|
|
|
// Act
|
|
var result = await controller.TryUpdateModelAsync(model);
|
|
|
|
// Assert
|
|
binder.Verify();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TryUpdateModel_UsesModelTypeNameIfSpecified()
|
|
{
|
|
var metadataProvider = new DataAnnotationsModelMetadataProvider();
|
|
var valueProvider = Mock.Of<IValueProvider>();
|
|
var binder = new Mock<IModelBinder>();
|
|
var modelName = "mymodel";
|
|
binder.Setup(b => b.BindModelAsync(It.IsAny<ModelBindingContext>()))
|
|
.Callback((ModelBindingContext b) =>
|
|
{
|
|
Assert.Equal(modelName, b.ModelName);
|
|
Assert.Same(valueProvider, b.ValueProvider);
|
|
})
|
|
.Returns(Task.FromResult(false))
|
|
.Verifiable();
|
|
var model = new MyModel();
|
|
var actionContext = new ActionContext(Mock.Of<HttpContext>(), new RouteData(), new ActionDescriptor());
|
|
var bindingContext = new ActionBindingContext(actionContext,
|
|
metadataProvider,
|
|
binder.Object,
|
|
valueProvider,
|
|
Mock.Of<IInputFormatterProvider>(),
|
|
Enumerable.Empty<IModelValidatorProvider>());
|
|
var bindingContextProvider = new Mock<IActionBindingContextProvider>();
|
|
bindingContextProvider.Setup(b => b.GetActionBindingContextAsync(actionContext))
|
|
.Returns(Task.FromResult(bindingContext));
|
|
var viewData = new ViewDataDictionary(metadataProvider, new ModelStateDictionary());
|
|
var controller = new Controller
|
|
{
|
|
ActionContext = actionContext,
|
|
BindingContextProvider = bindingContextProvider.Object,
|
|
ViewData = viewData
|
|
};
|
|
|
|
// Act
|
|
var result = await controller.TryUpdateModelAsync(model, modelName);
|
|
|
|
// Assert
|
|
binder.Verify();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TryUpdateModel_UsesModelValueProviderIfSpecified()
|
|
{
|
|
var metadataProvider = new DataAnnotationsModelMetadataProvider();
|
|
var valueProvider = Mock.Of<IValueProvider>();
|
|
var binder = new Mock<IModelBinder>();
|
|
var modelName = "mymodel";
|
|
binder.Setup(b => b.BindModelAsync(It.IsAny<ModelBindingContext>()))
|
|
.Callback((ModelBindingContext b) =>
|
|
{
|
|
Assert.Equal(modelName, b.ModelName);
|
|
Assert.Same(valueProvider, b.ValueProvider);
|
|
})
|
|
.Returns(Task.FromResult(false))
|
|
.Verifiable();
|
|
var model = new MyModel();
|
|
var actionContext = new ActionContext(Mock.Of<HttpContext>(), new RouteData(), new ActionDescriptor());
|
|
var bindingContext = new ActionBindingContext(actionContext,
|
|
metadataProvider,
|
|
binder.Object,
|
|
Mock.Of<IValueProvider>(),
|
|
Mock.Of<IInputFormatterProvider>(),
|
|
Enumerable.Empty<IModelValidatorProvider>());
|
|
var bindingContextProvider = new Mock<IActionBindingContextProvider>();
|
|
bindingContextProvider.Setup(b => b.GetActionBindingContextAsync(actionContext))
|
|
.Returns(Task.FromResult(bindingContext));
|
|
var viewData = new ViewDataDictionary(metadataProvider, new ModelStateDictionary());
|
|
var controller = new Controller
|
|
{
|
|
ActionContext = actionContext,
|
|
BindingContextProvider = bindingContextProvider.Object,
|
|
ViewData = viewData
|
|
};
|
|
|
|
// Act
|
|
var result = await controller.TryUpdateModelAsync(model, modelName, valueProvider);
|
|
|
|
// Assert
|
|
binder.Verify();
|
|
}
|
|
#endif
|
|
|
|
private class MyModel
|
|
{
|
|
public string Foo { get; set; }
|
|
}
|
|
}
|
|
}
|