views:

295

answers:

7
+2  Q: 

Hiding a function.

+15  A: 

You can use InternalsVisibleToAttribute to mark internal members as visible to your test assembly. It seems to shine when used in this context, though its not quite "friend".

  1. Mark your DangerousSet function internal instead of public.

  2. In Properties\AssemblyInfo.cs of the project containing DangerousSet:

    [assembly:InternalsVisibleTo("YourTestAssembly")]

If you have two test assemblies for whatever reason, the syntax is:

[assembly:InternalsVisibleTo("TestAssembly1"), 
    InternalsVisibleTo("TestAssembly2")]
cfeduke
A: 

Can your test code include a subclass of the calculations class? If so, you can mark the function protected and only inheritors will be able to use it. I'm pretty sure this also takes it out of intellisense, but I could be wrong about that.

Ryan
+5  A: 

Decorate your method with this attribute:

[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]

This will hide it from Intellisense.

EDIT: But apparently this has a rather significant caveat: "In Visual C#, EditorBrowsableAttribute does not suppress members from a class in the same assembly." Via MSDN.

Chris
This sounded like the perfect solution, but it still shows up.
Mark T
Oops, see edit for caveat.
Chris
+3  A: 

It sounds like your real problem is in your reference documents. You shouldn't test cases that are impossible to encounter under proper use of your class. If users shouldn't be allowed to change the state of those variables, then neither should your tests.

Bill the Lizard
One cannot change scientific source documents. They are what they are. They are written by the international experts in the field who are not concerned about programming (nor should they have to be).
Mark T
A: 

What I've done in the past is I put XML Comments by the method and used the section to write in big bold letters. DON'T USE THIS METHOD or whatever. That way, if someone tried to use it, Intellisense would give them a nice warning.

BFree
+1  A: 

You can also use reflection. Google search turned up Unit testing private methods using reflection.

Mufasa
+3  A: 

Suppose you want to test this object by manipulating its fields.

public class ComplexCalculation
{
    protected int favoriteNumber;
    public int FavoriteNumber
    {
        get { return favoriteNumber; }
    }
}

Place this object in your test assembly/namespace:

public class ComplexCalculationTest : ComplexCalculation
{
    public void SetFavoriteNumber(int newFavoriteNumber)
    {
        this.favoriteNumber = newFavoriteNumber;
    }
}

And write your test:

    public void Test()
    {
        ComplexCalculationTest myTestObject = new ComplexCalculationTest();
        myTestObject.SetFavoriteNumber(3);
        ComplexCalculation myObject = myTestObject;

        if (myObject.FavoriteNumber == 3)
            Console.WriteLine("Win!");

    }

PS: I know you said internal, but I don't think you meant internal.

David B