-1

Currently I wanted to do a UT coverage for which 100% function coverage is needed. I have a Public Class called FunctionParser.cs in my application and this internally uses an internal class called Autocomplete provided by Third party. Now the problem is there are few functionalities that lie within the AutoComplete Class which are untested . Therefore in order to do the coverage for this internal class named "AutoComplete" am trying to use reflection but nothing works for me can some one say me how can I use reflection to access this internal class for test purpose.

Note : I cannot modify the AutoComplete.CS which is an internal class provided by 3rd party, so other than using •Get [InternalsVisibleTo] . please suggest anything else that could work without modifying the third party class

4

2 回答 2

4
var innerType = Assembly.GetExecutingAssembly().GetTypes()
                .Where(t => t.DeclaringType == typeof(Outer))
                .First(t => t.Name == "Inner");

var innerObject = Activator.CreateInstance(innerType);
innerType.GetMethod("InnerTest", BindingFlags.Instance | BindingFlags.NonPublic)
         .Invoke(innerObject, new object[] { });

--

public class Outer
{
    class Inner
    {
        internal void InnerTest()
        {
            Console.WriteLine("test");
        }
    }
}
于 2012-09-07T11:18:42.910 回答
3

您不应该对第三方提供的代码进行单元测试。如果您的代码依赖于外部代码,则模拟该代码的行为。例如,外部库可能需要封装在实现特殊接口的附加类中。比将该接口注入到您的代码中,例如在构造函数中。在单元测试中,您将注入模拟实现。

于 2012-09-07T10:31:28.717 回答