Skip to content

Instantly share code, notes, and snippets.

@bradwilson
Last active January 4, 2016 12:59
Show Gist options
  • Save bradwilson/8625524 to your computer and use it in GitHub Desktop.
Save bradwilson/8625524 to your computer and use it in GitHub Desktop.
Testing ModelStateDictionary with xUnit.net v2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
public static class ModelStateDictionaryExtensions
{
public static IEnumerable<string> ToErrors(this ModelStateDictionary dict)
{
return dict.OrderBy(kvp => kvp.Key)
.SelectMany(kvp => kvp.Value.Errors.Select(e => Tuple.Create(kvp.Key, e.ErrorMessage)))
.Select(tuple => String.Format("{0} = {1}", tuple.Item1, tuple.Item2));
}
}
[Fact]
public void SampleTest()
{
var modelStateDictionary = SomethingUnderTest();
Assert.Collection(modelStateDictionary.ToErrors(),
error => Assert.Equal("key1 = The first value for key1"),
error => Assert.Equal("key1 = The second value for key1"),
error => Assert.Equal("key2 = The first value for key2")
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment