问题描述
在我的一些测试帮助程序代码中,我有一个名为FakeDbSet(Of T)
的IDbSet(Of T)
实现,该实现可在没有实际数据库的情况下模拟许多EF行为.我有一个声明为Friend
的类,因为我想强制所有代码像IDbSet(Of T)
那样与之交互.在内部(即,在我的Testing
程序集中),我访问了我想测试的课程的许多公共成员.
In some of my test helper code, I have a IDbSet(Of T)
implementation called FakeDbSet(Of T)
that mocks a lot of EF behavior without an actual database. I have the class declared Friend
because I want to force all code to interact with it like an IDbSet(Of T)
. Internally (ie within my Testing
assembly), I access a lot of the public members of the class which I want to be able to test.
问题是,我将单元测试保存在单独的程序集中Test.Unit
,这意味着它们无法访问我的内部实现!我知道我可以通过反射来解决这个问题,但是那很快就会变得麻烦和混乱.有什么更好的方法来设置我的项目来避免此问题?
The problem is, I keep my unit tests in a separate assembly Test.Unit
which means they cannot access my internal implementation! I know I can get around this with reflection, but that gets cumbersome and messy very quickly. Is there any better way to set up my project to avoid this problem?
推荐答案
将其添加到AssemblyInfo.cs文件中:
Add this to your AssemblyInfo.cs file:
[assembly:InternalsVisibleTo("YourOtherAssembly")]
这将使您的内部程序集对YourOtherAssembly
可见.但是,您应该真正考虑是否需要internal
.编写测试时,请尝试着重于public
部件.
This will make your internal assemblies visible to YourOtherAssembly
. However, you should really consider if you need to have it internal
. When writing tests try focusing on your public
parts.
Read more here: http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx
这篇关于单元测试一个类声明为朋友(内部)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!