If your logging aspect somewhat follows the PostSharp tutorials, then it should be writing to System.Diagnostics.Trace
.
Assuming that is true, you just need to configure your DI framework to not register a TextWriter (or System Logging, or similar...) in Trace when running your tests; this lets the aspects fire and behave as they normally would at runtime, and they still write to Trace, you just don't have all the disk IO going on (because you didn't register a Stream Writer in System.Diagnostics.Trace
).
I have used that successfully with my live logging aspect and not had issues with my SUTs or mocks. I also added a couple stubs with a couple simple methods, some decorated with aspects, some not. I then verified that the live aspect behaved as expected (Assert.Throws<MyWrappedException>
for example). These stubs allowed me to isolate down so that the Aspect was the SUT everything else was mocked, and I could assert the aspect behaved as I expected. That was a while ago, I probably could have used Moq instead of the fakes, but there has been no reason to change that code; they help verify my aspect was thread-safe.
As another option, you can add a stub or fake that inherits from OnMethodBoundaryAspect
, and have your CI register it as your Aspect at test runtime instead of your live aspect. This lets you leave the guts empty or near-empty if your live aspect isn't writing to Trace, or if you cannot exclude registering the System Logging or a TextWriter in Trace at test time. You then control the guts of the stubbed methods and decide what should be recorded in memory for later retrieval / assertion and what should just simply be ignored.
Because those methods are #protected
, Moq isn't going to be happy about it; To a pure Moq'ist this can feel painful or ugly, but, in this case you're talking about only one fake class, and it will coexist nicely with your Moqs. Additionally, it lets you isolate your tests to your SUTs ignoring the aspects, so it helps narrow your tests down to the class and behavior you care about.
Store that stubbed or fake class in the same project as your tests, and then configure your DI to instantiate your stub at test runtime instead of your real aspect. PostSharp will still properly inject into your SUTs at compile-time, and asserts should still succeed if you code the stub