Reputation: 180948
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?
Upvotes: 6
Views: 18014
Reputation: 532745
Make sure you've:
this
before the type to extendusing myns;
to any source file that uses the extension method if your extension method is contained inside a different namespaceNote 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.
Upvotes: 31
Reputation: 751
I could find very little documentation on Unit Testing for VB.NET. I had to discover by trial and error that, by default, Extension Methods in VB.NET only work in the current assembly. This is because a static class in VB.NET is a Module and Module scope defaults to "Friend" (Internal).
Use "Public Module" if you want to unit test your Extension Methods in a project referenced in a Unit Test project.
Upvotes: 0
Reputation: 23513
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.
Upvotes: 3
Reputation: 8304
The extension method should be in a static class. Where is your static class? Does your code compile?
Upvotes: 2