开发者

How do I test for TempData being set in MVC 2 where there is a redirect?

开发者 https://www.devze.com 2023-01-22 09:14 出处:网络
When trying to test my MVC 2 controllers, I am having a hard time testing the result of TempData when I\'m doing a redirect.It works ok if the result of the controller action is a ViewResult, however,

When trying to test my MVC 2 controllers, I am having a hard time testing the result of TempData when I'm doing a redirect. It works ok if the result of the controller action is a ViewResult, however, in a redirect, it is RedirectToRouteResult.

So my test is something like this:

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".

One option is to do the following:

Assert.That(controller.TempData.ContainsKey("info"));

If the result had been a ViewResult it could have been tested like this:

var viewResult = (ViewResult)actionResult;
Assert.That(viewResult.TempData.ContainsKey("info"));

Is there a way to test RedirectToRouteResult the same way as the ViewRes开发者_高级运维ult can be tested?

Thanks


Assert.That(controller.TempData.ContainsKey("info")); is exactly what you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消