Madcat.exe
Madcat.exe

Reputation: 17

Making a method only available to my tests

I'm trying to create a REST API library. I'm looking to find a way to expose just one method, in this case a method serving to delete objects I've used to test, to only my unit tests.

Basically this is just to clean up after each test. It also makes no sense to expose this delete method to any of my clients and is actually dangerous (deletes should be manual).

I found

[assembly: InternalsVisibleTo("FunctionalTests")] 

But it doesn't seem to affect the visibility of the method. Any ideas? I couldn't find any other questions addressing this specific issue.

Thanks!

Edit 1: The method in question is already marked internal. The class is public and has a public interface that does not contain the method in question.

public class Access: IAccess, IDisposable

Which contains the method:

 internal void DeleteIssue(string issueKey)

Yet it cannot be accessed by calling

AccessInstance.DeleteIssue(TestIssueKey);

It returns an error "Access does not contain a definition for 'DeleteIssue' accepting a first argument of type 'Access' could be found (are you missing a using directive or an assembly reference?)" I am in the same Namespace.

Upvotes: 0

Views: 300

Answers (2)

LB2
LB2

Reputation: 4860

[assembly: InternalsVisibleTo("FunctionalTests")] should be declared in AssemblyInfo.cs for it to work.

Upvotes: 0

NDJ
NDJ

Reputation: 5194

Make sure your methods you want to use are declared internal - private methods won't be exposed outside of the assembly even with InternalsVisibleTo:

From MSDN

Ordinarily, types and members with internal scope (in C#) and friend scope (in Visual Basic) are visible only in the assembly in which they are defined. The InternalsVisibleToAttribute attribute makes them also visible to the types in a specified assembly, which is known as a friend assembly.

Upvotes: 2

Related Questions