I am looking into the best ways to unit test my MVC 3 controllers. I was thinking of taking the result of viewresult on executing the controller action with a bunch of different params, serializing it and saving to file as a base for future tests.
2 questions:
- Is this a bad idea? for prior applications this would seem one of the safest ways to check that a change has not broken anything. I could deserialize my stored results, make any necessary changes and then compare to live results.
- If its a good way of testing, how can i serialize the viewresult? In the code below i get an error that the ActionResult cannot be serialized.
//create viewresult to return to view
ActionResult viewResult = View(dv);
//save viewresult for future unit test comparisons.
//Save data as name of controller action and param value used
string fileName = logDir + "\\" + controllerActionName + tradeDate.ToString("MMddyyyy") + ".viewresult";
//serialze and save to file
System.IO.Stream stream = System.IO.File.Open(fileName,System.IO.FileMode.Create);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bFormatter.Serialize(stream, viewResult);
stream.Close();
//send viewresult to mvc3 view
return viewResult;