15.3. Solution
Let's go ahead and create a controller with two actions:
public class TrackerController : Controller { [AcceptVerbs(HttpVerbs.Get)] public ActionResult DynamicImage(string email, int? messageId) { return null; } [AcceptVerbs(HttpVerbs.Get)] public ActionResult Link(string url, string email, int? messageId) { return null; } }
We will start our solution by creating a test to make sure that an image is returned even if no parameters are defined. This will ensure that the users don't get a broken image in their email. Here is the test:
[Test] public void dynamicimage_should_return_image_even_when_no_parameters_defined() { var con = new TrackerController(); var result = con.DynamicImage(null); Assert.IsNotNull(result,"Result is null"); Assert.IsInstanceOfType(typeof(FileContentResult), result, "Wrong type returned"); var fileContent = result as FileContentResult; Assert.AreEqual("tracker.jpg", fileContent.FileDownloadName, "Wrong file name"); Assert.AreEqual("image/jpeg", fileContent.ContentType, "Wrong content type"); }
This test basically calls the DynamicImage action and makes sure that the returned value is of type FileContent and that the filename and file content type are correct. In order to pass this test, we create an action in the TrackerController class as follows:
public class TrackerController : Controller { [AcceptVerbs(HttpVerbs.Get)] public ActionResult DynamicImage(MessageAudit messageAudit) { var content = System.IO.File.ReadAllBytes (Server.MapPath("~/content/tracker.jpg")); ...
Get ASP.NET MVC 1.0 Test Driven Development: Problem - Design - Solution now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.