0

Still not comfortable with all the enumerables out there. I'm trying to do this:

 Assert.IsTrue(actionResult.ViewData.ModelState.IsValid, null, Enumerable.ToArray<object>(actionResult.ViewData.ModelState as IEnumerable<object>));

It's an mbUnit assert with the following signature.

public static void IsTrue(bool actualValue, string messageFormat, params object[] messageArgs);

The third parameters causes (translated to english)

System.ArgumentNullException: Value cannot be null. Parameter name: source at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source) at Coin.UnitTests.AccountControllerTests.MyTest() in D:...\Tests\MbUnitTests\ControllerTests.cs:row 85

in Gallio. How do you do it?

Btw, does anybody know how to get these messages in English? Vista is in Swedish.

Martin
  • 2,956
  • 7
  • 30
  • 59

1 Answers1

3

ModelState does not implement IEnumerable<T> so the cast ends up being null and Enumerable.ToArray() doesn't like nulls, hence the exception.

Try something like this:

var errors = actionResult.ViewData.ModelState.Errors.Select(e => e.ErrorMessage).ToArray();
Assert.IsTrue(actionResult.ViewData.ModelState.IsValid, string.Join("\n", errors));
Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
  • 1
    Well sort of. After a bit of fiddleing I turned up this (which compiles, could be different version MCV?): var errors = actionResult.ViewData.ModelState.Values.Select(e => e.Errors.First().ErrorMessage).ToArray(); Assert.IsTrue(actionResult.ViewData.ModelState.IsValid, string.Join("\n", errors)); It works. Wonder if it could be made prettier? – Martin Jan 21 '10 at 01:30