How can I check the public member access method

So I have something like that

private List<ConcurrentQueue<int>> listOfQueues = new List<ConcurrentQueue<int>>()
public void InsertInt(int id, int value)
{
    listOfQueues[id].Enqueue(value);
}

Isn't it that I shouldn't be unit testing?
If so, how can I test the InsertInt method without using any other methods? can I use the method to retrieve data from the queue to check if it was entered correctly? For this, should I use mocks?

+3
source share
6 answers

. , ( ) . , , , .

, , unit test, , InsertInt() int listOfQueues. , , List<ConcurrentQueue<int>> Dictionary<string, ConcurrentQueue<int>>. , , InsertInt() unit test , .

, , InsertInt() , , , InsertInt() . , . , .

, , , , , , .

+2

, unit test . private accessor listOfQueues. unit test, .

, unit test http://msdn.microsoft.com/en-us/library/ms184807(v=vs.80).aspx

+2

, .

, -1, id ArgumentOutOfRangeException. , unit test ( , ).

, , , @Oskar Kjellin.

, Reflection, , .

// Sample with Queue instead of ConcurrentQueue
private void TestInsertInt(int id, int value)
{
    myInstance.InsertInt(id, value);

    FieldInfo field = myInstance.GetType().GetField("listOfQueues", BindingFlags.NonPublic | BindingFlags.Instance);
    List<Queue<int>> listOfQueues = field.GetValue(myInstance) as List<Queue<int>>;
    int lastInserted = listOfQueues[id].Last();

    Assert.AreEqual(lastInserted, value);
}
+1

- , , . , ConcurrentQueue , , , .

, , , . - ,

public int GetInt(int id)

, , , .

, , , . , , , :

        [TestCase(1,2,3)] // whatever test cases make sense for you
        [TestCase(4,5,6)]
        [TestCase(7,8,9)]
        [Test]
        public void Test_InsertInt_OK( int testId, int testValue, int expectedValue)
        {
            InsertInt(testId, testValue);
            Assert.AreEqual( GetInt(testId), expectedValue )
        }
+1

?

100% - ,

, InsertInt - ?

:

  • , .
  • unit test
  • , , unit test -
  • Use reflection to access private members.
0
source

Another hack is using PrivateObject .

Personally, I would either use DI (Injection Dependency) or do privateit internalfor Unit Testing!

0
source

All Articles