forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionaryModelBinderTest.cs
More file actions
54 lines (48 loc) · 2.21 KB
/
Copy pathDictionaryModelBinderTest.cs
File metadata and controls
54 lines (48 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) .NET Foundation. 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.Web.Http.Controllers;
using System.Web.Http.Metadata.Providers;
using System.Web.Http.Util;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.ModelBinding.Binders
{
public class DictionaryModelBinderTest
{
[Fact]
public void BindModel()
{
// Arrange
Mock<IModelBinder> mockKvpBinder = new Mock<IModelBinder>();
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(IDictionary<int, string>)),
ModelName = "someName",
ValueProvider = new SimpleHttpValueProvider
{
{ "someName[0]", new KeyValuePair<int, string>(42, "forty-two") },
{ "someName[1]", new KeyValuePair<int, string>(84, "eighty-four") }
}
};
HttpActionContext context = ContextUtil.CreateActionContext();
context.ControllerContext.Configuration.Services.Replace(typeof(ModelBinderProvider), (new SimpleModelBinderProvider(typeof(KeyValuePair<int, string>), mockKvpBinder.Object)));
mockKvpBinder
.Setup(o => o.BindModel(context, It.IsAny<ModelBindingContext>()))
.Returns((HttpActionContext cc, ModelBindingContext mbc) =>
{
mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
return true;
});
// Act
bool retVal = new DictionaryModelBinder<int, string>().BindModel(context, bindingContext);
// Assert
Assert.True(retVal);
var dictionary = Assert.IsAssignableFrom<IDictionary<int, string>>(bindingContext.Model);
Assert.NotNull(dictionary);
Assert.Equal(2, dictionary.Count);
Assert.Equal("forty-two", dictionary[42]);
Assert.Equal("eighty-four", dictionary[84]);
}
}
}