I'm trying to understand test-based development, and I wonder if these unit tests are good. I have an interface that 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 that implements this 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); } }
Those are the unit tests that I have written so far, are they beautiful or should I do something else? Should I mock 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!
source share