简体   繁体   English

为库的内部类编写UnitTests

[英]Write UnitTests for Internal Class of a library

I have the below class in the library i have to write UnitTests for and was given by my dev team to write unit tests for it. 我在库中有下面的类,我必须为此编写UnitTests,并且由我的开发团队给它编写了单元测试。

internal sealed class Settings : AppSettingsBase
{
    private static Settings defaultInstance = ((Settings)(Synchronized(new Settings())));

    public static Settings Default { get { return defaultInstance; } }
}

Since its internal i couldn't access it in my .Tests project. 由于其内部,我无法在我的.Tests项目中访问它。 When i looked in the stackoverflow the suggestion was to ask the developer to put an attribute [assembly:InternalsVisibleTo("MyTests")] in the AssemblyInfo.cs file. 当我查看stackoverflow时,建议是要求开发人员在AssemblyInfo.cs文件中放置一个属性[assembly:InternalsVisibleTo(“ MyTests”)]。 This is what i am going to request my developer but its going to take some time. 这是我要向开发人员提出的要求,但是要花一些时间。

Is there a way to do it through any reflection concept or any other concept to read those properties or variables. 有没有一种方法可以通过任何反射概念或任何其他概念来读取这些属性或变量。 I am using Microsoft FAkes in my test project. 我在测试项目中使用Microsoft FAkes。

The purpose of unit tests is to test publicly accessible aspects of the code. 单元测试的目的是测试代码的公共可访问方面。 As it's internal, it shouldn't be directly unit tested. 由于它是内部的,因此不应直接对其进行单元测试。 Test the public functionality that uses this class instead. 测试使用此类的公共功能。

If you must test it, then the solution is indeed to add [assembly:InternalsVisibleTo("MyTests")] to the assembly. 如果必须测试它,那么解决方案的确是将[assembly:InternalsVisibleTo("MyTests")]到程序集。 This doesn't take long. 这不会花很长时间。 It simply requires a quick edit to the AssemblyInfo.cs file. 它只需要快速编辑AssemblyInfo.cs文件即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM