C# 如何测试内部类库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15440935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:50:18  来源:igfitidea点击:

How to test internal class library?

c#unit-testingwrapperinternal

提问by Thomas Mondel

I would like to write a class library which creates for me a complex object but should only be exposed as little as possible. I want it to be included into other projects and there I only have one call to this library which e.g. returns me an object of a internally created class. I don't want to allow others to create these objects explicitly, but still I want to create a test project for this class library.

我想编写一个类库,它为我创建一个复杂的对象,但应该尽可能少地公开。我希望它被包含到其他项目中,并且我只有一个对该库的调用,例如返回一个内部创建类的对象。我不想让其他人显式地创建这些对象,但我仍然想为这个类库创建一个测试项目。

For example:

例如:

var result = Manager.Instance.Create(definition)

This should be the only access to the class library.

这应该是对类库的唯一访问。

Based on the definition parameter it uses different sub classes to create the requested instance and sets its properties accordingly. Therefore I want to assure somehow by tests that the whole creation process worked fine. But since I also don't want to expose very little internal properties of the result object too I cannot test by only using this public access method since I don't have any properties to assert on.

根据定义参数,它使用不同的子类来创建请求的实例并相应地设置其属性。因此,我想通过测试以某种方式确保整个创建过程运行良好。但由于我也不想公开结果对象的很少内部属性,因此我无法仅使用此公共访问方法进行测试,因为我没有任何要断言的属性。

I know that you should not test for internal mechanics and it is typically bad design and I also was reading through this article, but isn't there maybe any way to create a library plus unit test project and maybe afterwards limit the access to this class? with a wrapper or something?

我知道你不应该测试内部机制,它通常是糟糕的设计,我也在通读这篇文章,但也许没有任何方法可以创建一个库和单元测试项目,然后限制对此类的访问? 用包装纸什么的?

采纳答案by Wouter de Kort

In .NET can use the InternalsVisibleToAttributein your class library to make your internal types visible to your unit test project.

在 .NET 中可以使用 类库中的InternalsVisibleToAttribute使您的内部类型对您的单元测试项目可见。

That way you can keep your class internal and still use it from other assemblies that you give access.

这样您就可以将您的类保留在内部,并且仍然可以从您授予访问权限的其他程序集中使用它。

You use it like this:

你像这样使用它:

[assembly:InternalsVisibleTo("NameOfYourUnitTestProject")]

回答by Feiyu Zhou

For the latest csproj 2017 formated projects, if your project does not have the AssemblyInfo.csfile, you can add the following setting:

对于最新的csproj 2017格式化项目,如果你的项目没有该AssemblyInfo.cs文件,可以添加如下设置:

  <ItemGroup>
    <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
      <_Parameter1>$(MSBuildProjectName).Tests</_Parameter1>
    </AssemblyAttribute>
  </ItemGroup>

You also can use other variables to relplace MSBuildProjectNamesuch as AssemblyNameor use the unittest project name directly.

您也可以使用其他变量来替换MSBuildProjectName例如AssemblyName或直接使用 unittest 项目名称。

You can check the ProjectName.AssemblyInfo.csin obj folder (obj\Debug\netstandard2.0) has been updated by adding InternalsVisibleTo.

您可以检查ProjectName.AssemblyInfo.csOBJ文件夹中(obj\Debug\netstandard2.0)已加入更新InternalsVisibleTo