Question

Dependency injection doesn't work in Postsharp. How do I make aspects testable? I really want to avoid creating concrete classes for logging in my aspects.

I don't want the aspects running when I'm unit testing methods. If I'm testing a method I don't want the logging aspect firing and trying to create the concrete logger during my tests.

I love the idea of Postsharp and really want to use it, but these two problems seem to be causing too much trouble for me.

Any ideas?

Was it helpful?

OTHER TIPS

For certain third-party tools, such as log4net, the configuration language is powerful enough that you could configure (for example) the logging aspect one way in your unit test project, and another way in your actual projects. Thus, the logging aspect runs even in your unit test, but may actually do nothing.

For other tools, or your own custom solutions, you're better off programming to an interface inside the aspect logic. Then, based on static configuration files or project-specific setup code, you can specify one implementation of your interface for the production code, and another for the unit test project (presumably a stub implementation that has no side effects or better yet, records what would have happened for potential verification by the unit test).

Even better: you can disable PostSharp in a unit test project by using an MSBuild command. See this discussion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top