How do I test ActionFilterAttributes that work with ModelState?

Posted by Tomas Lycken on Stack Overflow See other posts from Stack Overflow or by Tomas Lycken
Published on 2010-04-10T20:28:24Z Indexed on 2010/04/10 20:33 UTC
Read the original article Hit count: 352

As suggested by (among others) Kazi Manzur Rashid in this blog post, I am using ActionFilterAttributes to transfer model state from one request to another when redirecting.

However, I find myself unable to write a unit test that test the behavior of these attributes. As an example, this what I want the test for the ImportModelStateAttribute to do:

  1. Setup the filterContext so that TempData[myKey] contains some fake "exported" ModelState (that is, a ModelStateDictionary I create myself, and add one error to)
  2. Make ModelState contain one model error.
  3. Call OnActionExecuting.
  4. Verify the two dictionaries are merged, and ModelState now contains both errors.

I'm at a loss already on the second step.

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc