Updated 18/09/2024
I have a custom AuthorizeAttribute called FooAttribute, this is my SUT (software under test) and its core implementation is not important for this post but the fact that the base class AuthorizeAttribute has protected virtual keywords before the method IsAuthorized is.
1 | protected virtual bool IsAuthorized(HttpActionContext actionContext) |
So the custom FooAttribute looks something like this, it has some logic in IsAuthorized that I want to unit test because its important that this code behaves as expected and doesnt change.
1 | public class FooAttribute : AuthorizeAttribute |
The noob would just try something like the below but this would protest as FooAttribute.IsAuthorized(HttpActionContext) is inaccessible due to its protection level, which is to be expected 🐒
1 | [] |
Work Around
A work around is just to add some additional setup to expose ‘protected’ behaviour in the test.
- Add the additional setup
ExposedFooAttributeto the test class file
1 | /// <summary> |
- Change the noob test above to use the exposed method as the entry point
1 | public void IsAuthorized_GivenWhenThen() |
This works because a protected member is accessible within its class and by derived class instances, so we are taking advantage of how the .Net framework behaves.
So now you can test IsAuthorized in the unit tests and trust the code in the future.
I only trust Batman ❤️