1: ///
2: /// This is a custom trace listener that loads the trace information into a
3: /// list of entries in the log file.
4: /// Found here: http://blogs.msdn.com/ploeh/archive/2006/04/06/UnitTestYourEnterpriseLibraryLoggingLogic.aspx
5: /// Author: Mark Seemann
6: ///
7: [ConfigurationElementType(typeof(CustomTraceListenerData))]
8: public class StubTraceListener : CustomTraceListener
9: {
10: private readonly static List logEntries_ = new List();
11: private readonly static List logMessages_ = new List();
12:
13: public override void Write(string message)
14: {
15: StubTraceListener.logMessages_.Add(message);
16: }
17:
18: public override void WriteLine(string message)
19: {
20: StubTraceListener.logMessages_.Add(message);
21: }
22:
23: public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
24: {
25: LogEntry le = data as LogEntry;
26:
27: if (le != null)
28: {
29: StubTraceListener.logEntries_.Add(le);
30:
31: if (this.Formatter != null)
32: {
33: this.Write(this.Formatter.Format(le));
34: return;
35: }
36: }
37:
38: base.TraceData(eventCache, source, eventType, id, data);
39: }
40:
41: internal static IList GetLogMessages()
42: {
43: return new ReadOnlyCollection(StubTraceListener.logMessages_);
44: }
45:
46: internal static IList GetLogEntries()
47: {
48: return new ReadOnlyCollection(StubTraceListener.logEntries_);
49: }
50:
51: internal static void Reset()
52: {
53: StubTraceListener.logEntries_.Clear();
54: StubTraceListener.logMessages_.Clear();
55: }
56: }
1: [TestMethod()]
2: public void WarningTest()
3: {
4: EntLibLogger target = new EntLibLogger();
5: string message = "warning message";
6: LogPriority priority = LogPriority.Low;
7: CallingMethod callingMethod = new CallingMethod();
8:
9: target.Warning(message, priority, callingMethod);
10: IList entries = StubTraceListener.GetLogEntries();
11:
12: Assert.IsNotNull(entries);
13: Assert.AreEqual(1, entries.Count);
14: Assert.AreEqual((int)priority, entries[0].Priority);
15: Assert.AreEqual(message, entries[0].Message);
16: }
I'm not testing much, but you can extend your test cases to test as much as you need. Just make sure you have an app.config file in your test project liks so:
And that you've used the LAB to setup your custom trace listener:
No comments:
Post a Comment