Why is my extension method not showing up in my test class?

I created an extension method HasContentPermissionon 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 this way:

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

It works like a charm. Now I want to unit test it. To do this, I really just need to call the extension method, therefore:

using System.Security.Principal;

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

But HasContentPermissionthere will be no intellisense. I tried to create a stub class that inherits from IIdentity, but that didn't work either. Why?

Or am I mistaken about this?

+5
source share
3 answers

Make sure you have:

  • made class static
  • made the class available to the calling code
  • included thisbefore type expansion
  • built a project containing an extension
  • ​​ unit test
  • using mypackage; , ,

, ( ) , .

, .NET. , , , , .

+14

, , System.Security.Principal. , , , .

+3

The extension method must be in a static class. Where is your static class? Does your code contain?

+2
source

All Articles