Monday, January 18, 2016

Unit Test on Private Methods in C#

There is an object in Microsoft.VisualStudio.TestTools.UnitTesting (Not sure about NUNIT) called PrivateObject 

Usage:

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tests
{
    [TestClass]
    public class Test1
    {
        [TestMethod]
        public void NoDuplicateTest()
        {
            string[] fileList = Directory.GetFiles(@"\\TestData\NoDuplicates");
            string backUpFolder = @"\\TestData\NoDuplicates\Sent\";

            PrivateObject privateMethod = new PrivateObject(new ClassName(string.Empty,string.Empty));
            var result = privateMethod.Invoke("MethodName", new[] {fileList.GetType(), backUpFolder.GetType()}, new object[] {fileList, backUpFolder});
            
            Assert.IsTrue((bool)result);
         }
    }
}


0 Comments:

Post a Comment

<< Home