I’m trying to grasp test driven development, and I’m wondering if those unit tests is fine. I have a interface which looks like this:
public interface IEntryRepository { IEnumerable<Entry> FetchAll(); Entry Fetch(int id); void Add(Entry entry); void Delete(Entry entry); }
And then this class which implements that interface:
public class EntryRepository : IEntryRepository { public List<Entry> Entries {get; set; } public EntryRepository() { Entries = new List<Entry>(); } public IEnumerable<Entry> FetchAll() { throw new NotImplementedException(); } public Entry Fetch(int id) { return Entries.SingleOrDefault(e => e.ID == id); } public void Add(Entry entry) { Entries.Add(entry); } public void Delete(Entry entry) { Entries.Remove(entry); } }
Theese are the unit tests I have written so far, are they fine or should I do something different? Should i be mocking the EntryRepository?
[TestClass] public class EntryRepositoryTests { private EntryRepository rep; public EntryRepositoryTests() { rep = new EntryRepository(); } [TestMethod] public void TestAddEntry() { Entry e = new Entry { ID = 1, Date = DateTime.Now, Task = 'Testing' }; rep.Add(e); Assert.AreEqual(1, rep.Entries.Count, 'Add entry failed'); } [TestMethod] public void TestRemoveEntry() { Entry e = new Entry { ID = 1, Date = DateTime.Now, Task = 'Testing' }; rep.Add(e); rep.Delete(e); Assert.AreEqual(null, rep.Entries.SingleOrDefault(i => i.ID == 1), 'Delete entry failed'); } [TestMethod] public void TestFetchEntry() { Entry e = new Entry { ID = 2, Date = DateTime.Now, Task = 'Testing' }; rep.Add(e); Assert.AreEqual(2, rep.Fetch(2).ID, 'Fetch entry failed'); } }
Thanks!
Just off the top of my head…
Although your testing of add really only tests the framework:
likewise with your fetch:
There’s a good checklist in the book Pragmatic Unit Testing (good book, highly recommended)