Eilon was correct - they made it easier!
It's trivial to test TempData now. This is all that's needed to port the example from his earlier post to Preview 4 (thanks Ben):
[TestFixture] public class HomeControllerTests { [Test] public void IndexSavesUserIDToTempDataAndRedirects() { var homeController = new HomeController(); homeController.TempData = new TempDataDictionary(); var result = homeController.Index(); Assert.That(result.Values.ContainsKey("action")); Assert.That(result.Values["action"], Is.EqualTo("GreetUser")); Assert.That(homeController.TempData.ContainsKey("UserID")); Assert.That(homeController.TempData["UserID"], Is.EqualTo("user123")); } [Test] public void GreetUserWithNoUserIDRedirects() { var homeController = new HomeController(); homeController.TempData = new TempDataDictionary(); var result = homeController.GreetUser() as RedirectToRouteResult; Assert.That(result, Is.Not.Null); Assert.That(result.Values.ContainsKey("action")); Assert.That(result.Values["action"], Is.EqualTo("ErrorPage")); Assert.That(homeController.TempData, Is.Empty); } [Test] public void GreetUserWithUserIDCopiesToViewDataAndRenders() { var homeController = new HomeController(); homeController.TempData = new TempDataDictionary(); homeController.TempData["UserID"] = "TestUserID"; var result = homeController.GreetUser() as ViewResult; Assert.That(result, Is.Not.Null); Assert.That(result.ViewName, Is.EqualTo("GreetUser")); Assert.That(result.ViewData, Is.Not.Null); Assert.That(result.ViewData.ContainsKey("NewUserID")); Assert.That(result.ViewData["NewUserID"], Is.EqualTo("TestUserID")); } }
If you actually need to test TempData, you can implement a stub ITempDataProvider and set the TempDataProvider propery on the controller:
public class TestTempDataProvider : ITempDataProvider { public TempDataDictionary TempDataDictionary { get; private set; } public TempDataDictionary LoadTempData() { return TempDataDictionary; } public void SaveTempData(TempDataDictionary tempDataDictionary) { TempDataDictionary = tempDataDictionary; } }
(Edited 6/17 with the proper usage from Ben)
4 Responses
Ben Scheirman
17|Jul|2008 1Also note that if you just want to test a controller that _uses_ temp data, you don't even have to do this. You can just say controller.TempData = new TempDataDictionary(); <– note no httpcontext parameter in constructor. Previously we had to provide this all mocked out.
Matt
17|Jul|2008 2Oh yeah. That's sort of what I was going for and I missed the boat. I'll update my post.
Dew Drop - July 17, 2008 | Alvin Ashcraft's Morning Dew
17|Jul|2008 3[...] Testing TempData in ASP.NET MVC Preview 4 (Matt Hinze) [...]
Zuhaib | ASP.NET MVC Preview 4 Released
18|Jul|2008 4[...] Finally TempData is test-friendly by Matt Hinze [...]
Leave a reply
Search
Categories
Archives
Links
Recent Posts
Tags
.htaccess 443 Agile asp.net ASP.NET MVC aspnetmvc auditing business career certificate codecampserver continuous improvement culture ddd delicious dependency injection dry fluent interface headspring iis iis 6 linq log4net logging mvc nhibernate ninject refactoring resharper sed self selfssl seo sql ssl stats subcontrollers tabs tdd tempdata tortoisesvn treesurgeon visual studio whitespace wordpress