13.3. Solution
We will start off with a simple test to get the list of system templates. We will mock the call to the service class to return some Template objects:
[Test] public void list_returns_system_templates() { var mockSer = new Mock<ITemplateService>(); mockSer.Expect(s =< s.Get()).Returns(new List<Template> { new Template(), new Template() }); var con = new TemplateController(mockSer.Object); var result = con.List(); Assert.IsNotNull(result); result.AssertViewResult(con, null, "list"); Assert.IsInstanceOfType(typeof(IList<Template>), con.ViewData.Model); Assert.AreEqual(4, (con.ViewData.Model as IList<Template>).Count); }
Next step, let's make the solution compile. Create the controller with our List action as follows:
public class TemplateController : Controller { [AcceptVerbs(HttpVerbs.Get)] [Authorize] public ActionResult List() {
return null; } }
Create the model class Template:
public class Template { }
Let's add the extra plumbing to make it pass the test. Add and initialize the template service with the TemplateController constructor:
public ITemplateService Service { get; set; } public TemplateController():this(null) { } public TemplateController(ITemplateService service) { Service = service; }
The test should now pass. It is a good idea to see how things are looking with a view to make sure that things are working. Let's create a test for the service class. We will go ahead and mock the repository interface:
[Test] public void get_should_return_list_of_templates() ...
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.