Carpool Project: Part #4
October 13, 2010 Leave a comment
I wrote this factory code:
1 public SwimTeam GetSwimTeam(int swimTeamId) 2 { 3 var selectedSwimTeam = (from swimTeam in carpoolEntity.Carpool_SwimTeam 4 where swimTeam.SwimTeamId == swimTeamId 5 select swimTeam).First(); 6 7 return MapSwimTeam(selectedSwimTeam); 8 9 } 10 11 public List<SwimTeam> GetAllSwimTeams() 12 { 13 List<SwimTeam> swimTeams = new List<SwimTeam>(); 14 15 var swimTeamsQuery = (from swimTeam in carpoolEntity.Carpool_SwimTeam 16 select swimTeam); 17 18 foreach(Carpool_SwimTeam carpool_Swimteam in swimTeamsQuery) 19 { 20 swimTeams.Add(MapSwimTeam(carpool_Swimteam)); 21 } 22 23 return swimTeams; 24 } 25
How do I know that I wrote it right?
Unit Test? Integration Test? I don’t care what you call it – I want to make sure it is right BEFORE I use this as a pattern for the rest of the project.
So I created a couple of unit tests (and remembering to add the app.config to my unit test project)
1 [TestMethod()] 2 public void GetSwimTeamTest() 3 { 4 string expected = "RSA"; 5 string actual = SwimTeamFactory.GetSwimTeam(1).Name; 6 Assert.AreEqual(expected, actual); 7 } 8 9 /// <summary> 10 ///A test for GetAllSwimTeams 11 ///</summary> 12 [TestMethod()] 13 public void GetAllSwimTeamsTest() 14 { 15 int expected = 3; 16 int actual = SwimTeamFactory.GetAllSwimTeams().Count; 17 Assert.AreEqual(expected, actual); 18 } 19
And got green:
I then wired up the UI
I added a controller to my MVC application
1 public class SwimTeamController : Controller 2 { 3 SwimTeamFactory swimTeamFactory = null; 4 5 public SwimTeamController() 6 { 7 swimTeamFactory = new SwimTeamFactory(); 8 } 9 10 public ActionResult Index() 11 { 12 return View(swimTeamFactory.GetAllSwimTeams()); 13 } 14 15 } 16
Added the connection string to the Web.Config
Added an auto generated View
And boom goes the dynamite…