title: How do I test for TempData being set in MVC 2 where there is a redirect?
body: <p>When trying to test my MVC 2 controllers, I am having a hard time testing the result of <code>TempData</code> when I'm doing a redirect.  It works ok if the result of the controller action is a <code>ViewResult</code>, however, in a redirect, it is <code>RedirectToRouteResult</code>.</p>
  
  <p>So my test is something like this:</p>
  
  <pre><code>var controller = new SubscriptionController(this.dataStorageMock.Object)
      {
          ControllerContext = MvcMockHelpers.GetControllerContextMock("POST")
      };
  
  var actionResult = controller.Create(formCollection);
  var redirectResult = (RedirectToRouteResult)actionResult;
  
  // TODO: Need to ensure TempData contains a key "info".
  </code></pre>
  
  <p>One option is to do the following:</p>
  
  <pre><code>Assert.That(controller.TempData.ContainsKey("info"));
  </code></pre>
  
  <p>If the result had been a <code>ViewResult</code> it could have been tested like this:</p>
  
  <pre><code>var viewResult = (ViewResult)actionResult;
  Assert.That(viewResult.TempData.ContainsKey("info"));
  </code></pre>
  
  <p>Is there a way to test <code>RedirectToRouteResult</code> the same way as the <code>ViewResult</code> can be tested?</p>
  
  <p>Thanks</p>