如何在ASP.NET MVC3中测试ValueProviderFactories?
-
25-09-2019 - |
题
我们想将项目从ASP.NET MVC 2升级到3。我们的大多数测试都成功了,但是有些测试失败了 ValueProviderFactories.Factories.GetValueProvider(context)
.
这是一个简单的测试类,可以解决问题。
[TestFixture]
public class FailingTest
{
[Test]
public void Test()
{
var type = typeof(string);
// any controller
AuthenticationController c = new AuthenticationController();
var httpContext = new Mock<HttpContextBase>();
var context = c.ControllerContext = new ControllerContext(httpContext.Object, new RouteData(), c);
IModelBinder converter = ModelBinders.Binders.GetBinder(type);
var bc = new ModelBindingContext
{
ModelName = "testparam",
ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, type),
ValueProvider = ValueProviderFactories.Factories.GetValueProvider(context)
};
Console.WriteLine(converter.BindModel(context, bc));
}
}
异常“对象引用未设置为对象的实例”。在 ValueProviderFactories.Factories.GetValueProvider(context)
叫做。堆栈看起来像这样:
Microsoft.Web.Infrastructure.dll!Microsoft.Web.Infrastructure.DynamicValidationHelper.ValidationUtility.CollectionReplacer.GetUnvalidatedCollections(System.Web.HttpContext context) + 0x23 bytes
Microsoft.Web.Infrastructure.dll!Microsoft.Web.Infrastructure.DynamicValidationHelper.ValidationUtility.GetUnvalidatedCollections(System.Web.HttpContext context, out System.Collections.Specialized.NameValueCollection form, out System.Collections.Specialized.NameValueCollection queryString, out System.Collections.Specialized.NameValueCollection headers, out System.Web.HttpCookieCollection cookies) + 0xbe bytes
System.Web.WebPages.dll!System.Web.Helpers.Validation.Unvalidated(System.Web.HttpRequest request) + 0x73 bytes
System.Web.WebPages.dll!System.Web.Helpers.Validation.Unvalidated(System.Web.HttpRequestBase request) + 0x25 bytes
System.Web.Mvc.DLL!System.Web.Mvc.FormValueProviderFactory..ctor.AnonymousMethod__0(System.Web.Mvc.ControllerContext cc) + 0x5a bytes
System.Web.Mvc.DLL!System.Web.Mvc.FormValueProviderFactory.GetValueProvider(System.Web.Mvc.ControllerContext controllerContext) + 0xa0 bytes
System.Web.Mvc.DLL!System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider.AnonymousMethod__7(System.Web.Mvc.ValueProviderFactory factory) + 0x4a bytes
System.Core.dll!System.Linq.Enumerable.WhereSelectEnumerableIterator<System.Web.Mvc.ValueProviderFactory,<>f__AnonymousType2<System.Web.Mvc.ValueProviderFactory,System.Web.Mvc.IValueProvider>>.MoveNext() + 0x24d bytes
System.Core.dll!System.Linq.Enumerable.WhereSelectEnumerableIterator<<>f__AnonymousType2<System.Web.Mvc.ValueProviderFactory,System.Web.Mvc.IValueProvider>,System.Web.Mvc.IValueProvider>.MoveNext() + 0x2ba bytes
mscorlib.dll!System.Collections.Generic.List<System.Web.Mvc.IValueProvider>.List(System.Collections.Generic.IEnumerable<System.Web.Mvc.IValueProvider> collection) + 0x1d8 bytes
System.Core.dll!System.Linq.Enumerable.ToList<System.Web.Mvc.IValueProvider>(System.Collections.Generic.IEnumerable<System.Web.Mvc.IValueProvider> source) + 0xb5 bytes
System.Web.Mvc.DLL!System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(System.Web.Mvc.ControllerContext controllerContext) + 0x24d bytes
test.DLL!FailingTest.Test() Line 31 + 0xf9 bytes C#
我想知道为什么它引发异常并看到的原因:
public static ValidationUtility.UnvalidatedCollections GetUnvalidatedCollections(HttpContext context)
{
return (ValidationUtility.UnvalidatedCollections) context.Items[_unvalidatedCollectionsKey];
}
所以,当我们依赖时,我们是否回到过去 HttpContext.Current
?如何解决?
解决方案
可以通过访问httpcontext访问忽略它的httpcontext的估值来解决这一问题。
我已经在博客文章中解释了所有内容: ASP.NET MVC3中带有ValueProviderFactories的单位测试动作.
关键是此代码:
public static class ValueProviderFactoresExtensions {
public static ValueProviderFactoryCollection ReplaceWith<TOriginal>(this ValueProviderFactoryCollection factories, Func<ControllerContext, NameValueCollection> sourceAccessor) {
var original = factories.FirstOrDefault(x => typeof(TOriginal) == x.GetType());
if (original != null) {
var index = factories.IndexOf(original);
factories[index] = new TestValueProviderFactory(sourceAccessor);
}
return factories;
}
class TestValueProviderFactory : ValueProviderFactory {
private readonly Func<ControllerContext, NameValueCollection> sourceAccessor;
public TestValueProviderFactory(Func<ControllerContext, NameValueCollection> sourceAccessor) {
this.sourceAccessor = sourceAccessor;
}
public override IValueProvider GetValueProvider(ControllerContext controllerContext) {
return new NameValueCollectionValueProvider(sourceAccessor(controllerContext), CultureInfo.CurrentCulture);
}
}
}
因此可以用作:
ValueProviderFactories.Factories
.ReplaceWith<FormValueProviderFactory>(ctx => ctx.HttpContext.Request.Form)
.ReplaceWith<QueryStringValueProviderFactory>(ctx => ctx.HttpContext.Request.QueryString);
实际上非常简单:)
更新: :如评论中所述,您应该记住:
- 放
ctx.HttpContext.Request.ContentType
属性达到一些无关的值,否则JSONVALUEPROVIDERFACTORY将引发异常。我更喜欢在那里创建模拟并设置默认值。 - 更换
HttpFileCollectionValueProviderFactory
可以在绑定过程中使用。 - 当心项目中可能拥有的其他依赖项。
其他提示
您不应该从单元测试中调用ValueProviderFactories.FalcroviderFactories.Factries,ModelBinders.binders或任何其他静态访问者。这就是为什么ModelBindingContext.ValueProvider存在 - 这样您就可以提供自己创建的模拟的ivalueprovider,而不是依靠静态默认值(假设MVC管道正在运行)。
不隶属于 StackOverflow