Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Extension Method not showing up in my test class?

I created an extension method called HasContentPermission on the System.Security.Principal.IIdentity:

namespace System.Security.Principal
{
    public static class IdentityExtensions
    {
        public static bool HasContentPermission
            (this IIdentity identity, int contentID)
        {
            // I do stuff here
            return result;
        }
    }
}

And I call it like this:

bool hasPermission = User.Identity.HasPermission(contentID);

Works like a charm. Now I want to unit test it. To do that, all I really need to do is call the extension method directly, so:

using System.Security.Principal;

namespace MyUnitTests
{
    [TestMethod]
    public void HasContentPermission_PermissionRecordExists_ReturnsTrue()
    {
        IIdentity identity;
        bool result = identity.HasContentPermission(...

But HasContentPermission won't intellisense. I tried creating a stub class that inherits from IIdentity, but that didn't work either. Why?

Or am I going about this the wrong way?

like image 848
Robert Harvey Avatar asked Mar 26 '10 17:03

Robert Harvey


People also ask

Where do you put extension methods?

An Extension Method should be in the same namespace as it is used or you need to import the namespace of the class by a using statement. You can give any name of for the class that has an Extension Method but the class should be static.

Can you add extension methods to an existing static class?

No. Extension methods require an instance variable (value) for an object. You can however, write a static wrapper around the ConfigurationManager interface. If you implement the wrapper, you don't need an extension method since you can just add the method directly.

Can we add extension method to sealed class?

You can also add new methods in the sealed class also using an extension method concept. It cannot apply to fields, properties, or events.

Should I Unit Test extension methods C#?

Extension methods in C# are the easiest and most under-used way to implement the Open-Closed Principle. The tricky part is to debug bugs caused by Extension methods. The best way to prevent these bugs is to ensure Unit Tests are written for Extension methods.


2 Answers

Make sure you've:

  1. made the class static
  2. made the class accessible to the calling code
  3. included this before the type to extend
  4. built the project containing the extension
  5. added a reference to the project in your unit test project
  6. added a using mypackage; to any source file that uses the extension method if your extension method is contained inside a different package

Note that you've also got (I assume) a typo in your example in that the method isn't in a class.

Finally, I would avoid putting methods into the official .NET namespaces. It can only be confusing to anyone coming after you who might think that the method is an officially supported method when in reality it is your own and contained within your project.

like image 50
tvanfosson Avatar answered Sep 30 '22 13:09

tvanfosson


I suspect that it has something to do with the fact that you added an extension method to the existing System.Security.Principal namespace. Make sure that you reference the project that defines the extension method, or try it with a different namespace.

like image 24
Greg Avatar answered Sep 30 '22 14:09

Greg