Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7648905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:49:58+00:00 2026-05-31T10:49:58+00:00

After asking this question about implementing an aspect with PostSharp, it came to my

  • 0

After asking this question about implementing an aspect with PostSharp, it came to my mind that I might have to update the code of this aspect in the future, and that I did not want to take the risk of breaking everything afterwards.

So, I started thinking about unit testing.

My first question is:

Is it relevant to think about unit testing an aspect?

I would like the answer to be “yes”, but if not, I expect getting other advices.

And then, if so,

How to implement unit testing for PostSharp aspects?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:50:00+00:00Added an answer on May 31, 2026 at 10:50 am

    Yes it certainly makes sense to unit tests aspects, since they represent functionality and since you are using it in more than one place it is even more important to test it.

    However you have to devide this into two parts:

    1. Testing the actual aspect functionality
    2. Testing whether context extraction is working properly that is used to actually execute the aspect functionality

    For the first part, if you have decoupled the actual functionality from the attribute that executes the aspect functionality properly, creating unit tests should be no different than unit testing normal code.

    For the second part you need to decouple the extraction of context as well, this might seem like overkill but if you wanne unit test it properly you will need todo it I’m afraid.

    On that note you should also make use of compile time validation, which can also prevents you from using the attributes in the wrong way. Sometimes it’s necessary to test certain conditions that you can not describe with the Attribute syntax, then compile time validation comes into play. This has been a great asset for me and reduced the amount of debugging sessions in regards to PostSharp aspects significantly, see:
    http://www.sharpcrafters.com/postsharp/robustness

    Here is some very basic sample code, no DI nothing, just to illustrate how to split things up:

    public sealed class TraceAttribute : OnMethodBoundaryAspect
    {
        private readonly string category;
        private TraceArgumentService argumentService;
        private TraceService traceService;
    
        public string Category { get { return category; } }
    
        public TraceAttribute(string category)
        {
            this.category = category;
        }
    
        public override void RuntimeInitialize(System.Reflection.MethodBase method)
        {
            base.RuntimeInitialize(method);
            this.argumentService = new TraceArgumentService();
            this.traceService = new TraceService();
        }
    
    
        public override void OnEntry(MethodExecutionArgs args)
        {                
            traceService.Write(
                argumentService.GetDeclaringTypeName(args),
                argumentService.GetMethodName(args),
                category);
    
        }
    }
    
    public class TraceArgumentService
    {
        public string GetDeclaringTypeName(MethodExecutionArgs args)
        {
            return args.Method.DeclaringType.Name;
        }
    
        public string GetMethodName(MethodExecutionArgs args)
        {
            return args.Method.Name;
        }
    }
    
    public class TraceService
    {
        public void Write(string declaringTypeName, string methodName, string category)
        {
            Trace.WriteLine(string.Format("Entering {0}.{1}.",
                declaringTypeName, methodName), category);
        }
    }
    

    You might ask why TraceService and a separate TraceArgumentService:

    • The Tracing logic should be independent from PostSharp, therefore it should not be knowing about MethodExecutionArgs.
    • Extracting the arguments from MethodExecutionArgs is not part of Tracing, it’s more related to the aspect. Since you want to be able to test it, you need to somehow separate it.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After asking this question about interface fields in records I assumed that the following
After asking this question about authorization, I've added a new custom attribute in an
Please note that this is asking a question about constructors, not about classes which
I understand that this question is similar to others asking about Java Properties, and
Hello yeah I'm asking this question a second time, sorry about that but I
This might seem like a silly question, but after asking some questions on stackoverflow
after asking question about Instruments and finding problems with my code there are two
After asking this question and apparently stumping people, how's about this for a thought--
After asking this question yesterday about branching in mercurial, I've decided to try out
After asking this question I was advised not to check in binaries into subversion.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.