-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathTestLogAspect.cs
More file actions
51 lines (45 loc) · 1.26 KB
/
TestLogAspect.cs
File metadata and controls
51 lines (45 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace PostSharp.Samples.DependencyResolution.Contextual.Test
{
[TestClass]
public class TestLogAspect
{
[TestMethod]
public void TestMethod()
{
// The ServiceLocator can be initialized for each test.
using (
AspectServiceLocator.AddRule<ILogger>(
(type, member) =>
type == typeof(LogAspect) && member.Name == "TargetMethod" ? new TestLogger() : null)
)
{
TestLogger.Clear();
TargetMethod();
Assert.AreEqual("OnEntry" + Environment.NewLine, TestLogger.GetLog());
}
}
[LogAspect]
public void TargetMethod()
{
}
}
internal class TestLogger : ILogger
{
public static readonly StringBuilder stringBuilder = new StringBuilder();
public void Log(string message)
{
stringBuilder.AppendLine(message);
}
public static string GetLog()
{
return stringBuilder.ToString();
}
public static void Clear()
{
stringBuilder.Clear();
}
}
}