我正在为自定义模型绑定器(MVC 3)编写单元测试,并希望确认我通过 ValueProvider 传入的所有值都被绑定器使用(意味着绑定器读取它们并将它们绑定到模型对象)。(我还想要一个测试用例来确认没有消耗带有额外值的 ValueProvider)
这是我的测试的(工作)代码:
<Test()> _
Public Sub Should_Bind_Example_Payment_Method_To_Model()
'arrange
Dim testBindingContext As New ModelBindingContext() With {
.ModelName = "MyModel",
.ValueProvider = New NameValueCollectionValueProvider(
New NameValueCollection() From {
{"Param1", "val1"},
{"Param2", "val2"},
}, Nothing),
.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(Nothing, GetType(MyModelType))
}
dim testControllerContext as new ControllerContext()
Dim modelBinder As New MyCustomModelBinder()
'act
Dim generatedModel As MyModelType = modelBinder.BindModel(testControllerContext, testBindingContext)
'assert
Assert.AreEqual("val1", generatedModel.Param1)
Assert.AreEqual("val2", generatedPaymentMethod.Param2)
Assert.IsTrue(testBindingContext.ModelState.IsValid)
End Sub
我想添加这样一行(这显然是无效代码)
Assert.IsTrue(testBindingContext.ValueProvider.AllValuesConsumed)