I would restructure your class to make RaiseInvalidEntryEvent virtual so that it can be mocked by your IsValidEntry_WithInValidTicker, and then confirm that it was called when the ticket was invalid.
Then I will have another test that checks RaiseInvalidEntryEvent, which calls the anon delegate separately.
Unit testing should be as atomic as possible, and you would like to test both of these behaviors in different tests.
public delegate void OnInvalidEntryMethod(ITnEntry entry, string message); public class EntryValidator { public event OnInvalidEntryMethod OnInvalidEntry; public bool IsValidEntry(ITnEntry entry, string ticker) { if (!IsFieldValid(entry, ticker.Trim().Length.ToString(), "0")) return false; return true; } private bool IsFieldValid(ITnEntry entry, string actual, string invalidValue) { if (actual == invalidValue) { RaiseInvalidEntryEvent(entry); return false; } return true; } public virtual void RaiseInvalidEntryEvent(ITnEntry entry) { if (OnInvalidEntry != null) OnInvalidEntry(entry, "Invalid entry in list: " + entry.List.Name + "."); } }
NOTE. Some OOP evangelists adapt when things are declared public rather than private, mostly unit testing and TDD have some requirements that pure OOP disagrees with. I made RaiseInvalidEntryEvent publicly available for simplicity, but I usually do it internally and then expose the assembly to the unit test via InternalsVisibleTo. I have been involved in TDD for the past 4 years and rarely use private ones.
And unit tests will be fast (note: this uses the MSTEST structure from VS2012)
[TestClass] public class UnitTest1 { #region TestHelpers private ITnEntry MakeEntry(string ticker) { return new TnEntry {Ticker = ticker, List = new Ticket()}; } #endregion [TestMethod] public void IsValidEntry_WithValidValues_ReturnsTrue() {
source share