Reputation: 8785
I'm writing a unit test for a custom model binder (MVC 3) and want to confirm all the values I passed in through my ValueProvider are consumed by the binder (meaning the binder read them and bound them to the model object). (I also want a test case to confirm that a ValueProvider with extra values are not consumed)
Here's the (working) code for my test:
<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
I want to add a line like this (this is obviously invalid code)
Assert.IsTrue(testBindingContext.ValueProvider.AllValuesConsumed)
Upvotes: 0
Views: 879
Reputation: 3844
There are two ways:
A) Create a mock of IValueProvider that has the logic to test if you consume all.
B) Accept multible assertions, but extract them in to a method to keep the unittest short, simple and readable.
I would chose B) because of simplicity -- unless i expected similar tests involving IValueProvider. I would call the assertion method something like AssertThatAllValuesAreConsumed(testBindingContext)
.
Regards, Morten
Upvotes: 1