This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (var db = new QuesterContext()) | |
{ | |
db.Database.Migrate(); | |
} |
Next, the beginning of my LocationRepository class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LocationRepository | |
{ | |
public IEnumerable<Location> Get() | |
{ | |
return null; | |
} | |
} |
In my first test, I'll try to read from an empty table, so I have to clear all entries from it first. After all, I can't be sure that this test won't run after one that adds something to my Locations table.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestMethod] | |
public void Get_returns_an_empty_set_if_table_contains_no_entries() | |
{ | |
using (QuesterContext dbcontext = new QuesterContext()) | |
{ | |
// Clear the Locations table | |
dbcontext.RemoveRange(dbcontext.Locations); | |
dbcontext.SaveChanges(); | |
} | |
// Now test that Get() returns an empty set | |
LocationRepository locationRepository = new LocationRepository(); | |
var locations = locationRepository.Get().ToList(); | |
Assert.AreEqual(0, locations.Count); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LocationRepository | |
{ | |
public IEnumerable<Location> Get() | |
{ | |
IEnumerable<Location> result = null; | |
using (QuesterContext context = new QuesterContext()) | |
{ | |
result = from e in context.Locations | |
select e; | |
} | |
return result; | |
} | |
} |
Well, that's because I forgot that my LINQ query actually uses lazy evaluation, and by the time I get around to trying to use my result (in my test), my database context has already disposed of. So I do a quick fix on my Get() method to force the evaluation of my query while it's still in scope:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LocationRepository | |
{ | |
public IEnumerable<Location> Get() | |
{ | |
IEnumerable<Location> result = null; | |
using (QuesterContext context = new QuesterContext()) | |
{ | |
result = (from e in context.Locations | |
select e).ToList(); | |
} | |
return result; | |
} | |
} |
Now let's see how we fare with a table that's not empty:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestMethod] | |
public void Get_returns_all_stored_locations() | |
{ | |
// Create some locations | |
Location l1 = new Location(4); | |
Location l2 = new Location(2); | |
Location l3 = new Location(7); | |
// Set up the database context | |
using (QuesterContext dbcontext = new QuesterContext()) | |
{ | |
// Clear the Locations table | |
dbcontext.RemoveRange(dbcontext.Locations); | |
// Add the locations I prepared earlier | |
dbcontext.Locations.Add(l1); | |
dbcontext.Locations.Add(l2); | |
dbcontext.Locations.Add(l3); | |
// Save my changes to the locations table | |
dbcontext.SaveChanges(); | |
} | |
// Now test that Get() retrieves entries from the database correctly | |
LocationRepository locationRepository = new LocationRepository(); | |
var locations = locationRepository.Get().ToList(); | |
// Check that we have the correct number of locations | |
Assert.AreEqual(3, locations.Count); | |
} |
P.S. Remember: this is NOT a tutorial on how to write good code. This is an exploration of how useful TDD is in weeding out mistakes, bad code and possibly bad architecture. So I'm throwing everything at it, including the kitchen sink.
P.P.S. You may be able to see where this is going. I'm currently looking at the code I will be using over the next few posts, and I'm not certain if the wetness trickling down my face is tears or if my eyes are bleeding.
P.P.P.S But hey - "For Science!"