SQLite 程序集未复制到输出文件夹以进行单元测试
问题:在进行单元测试时,我的 DAL 程序集中引用的 SQLite 程序集不会复制到输出文件夹(复制本地设置为 true
)。
我正在 VS2008 中使用 NHibernate 和 NHibernate 开发 .Net 3.5 应用程序。我的 DAL 中的 SQLite。数据访问通过 IRepository 接口(存储库工厂)公开给其他层,因此无需引用其他层中的 NHibernate
或 System.Data.SQLite
程序集。
对于单元测试,有一个公共工厂方法(也在我的 DAL 中),它创建内存中 SQLite 会话并创建新的 IRepository 实现。这样做也是为了避免为所有需要它的程序集提供共享的 SQLite 内存配置,并避免引用这些 DAL 内部程序集。
问题是当我运行驻留在单独项目的单元测试时 - 如果我不添加 System.Data.SQLite 作为对单元测试项目的引用,它不会被复制到 TestResults ...\Out 文件夹(尽管此项目引用了我的 DAL 项目,该项目引用了 System.Data.SQLite,其 Copy 本地属性设置为 true),因此配置 NHibernate 时测试失败。如果我将引用添加到我的测试项目中,那么它就会被复制并且单元测试可以工作。
我做错了什么?
[更新]
我似乎在这里找到了答案:TFS UnitTesting 在构建服务器上时不部署本地副本程序集来测试目录。如果我在 DAL 中的某个静态方法中添加对该类型的引用,当我在测试中引用 DAL 程序集时,它将自动复制。这看起来确实像是一个黑客,但恕我直言,这是一个比使用单独的脚本更干净的解决方案,因为它创建了“真正的”依赖关系。
如果我将 SQLite 程序集作为附加部署项添加到我的测试运行配置(LocalTestRun.testrunconfig 文件),它似乎也会被复制。
感谢您的快速解答!
Problem: SQLite assembly referenced in my DAL assembly does not get copied to the output folder when doing unit tests (Copy local is set to true
).
I am working on a .Net 3.5 app in VS2008, with NHibernate & SQLite in my DAL. Data access is exposed through the IRepository interface (repository factory) to other layers, so there is no need to reference NHibernate
or the System.Data.SQLite
assemblies in other layers.
For unit testing, there is a public factory method (also in my DAL) which creates an in-memory SQLite session and creates a new IRepository implementation. This is also done to avoid have a shared SQLite in-memory config for all assemblies which need it, and to avoid referencing those DAL internal assemblies.
The problem is when I run unit tests which reside a separate project - if I don't add System.Data.SQLite
as a reference to the unit test project, it doesn't get copied to the TestResults...\Out folder (although this project references my DAL project, which references System.Data.SQLite
, which has its Copy local property set to true
), so the tests fail while NHibernate is being configured. If I add the reference to my testing project, then it does get copied and unit tests work.
What am I doing wrong?
[Update]
It seems I've found the answer here: TFS UnitTesting not deploying local copy assembly to test dir when on build server. If I add a reference to that type in some static method in my DAL, it will get copied automatically when I reference the DAL assembly in my tests. This does seem like a hack but IMHO is a cleaner solution than having a separate script since it creates a "real" dependency.
It seems that it also gets copied if I add the SQLite assembly as an additional deployment item to my test run configuration (LocalTestRun.testrunconfig file).
Thanks for your quick answers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 DAL 项目引用 System.Data.SQLite 程序集,但这并不意味着它将被复制到测试项目的输出文件夹,特别是如果它是由 NHibernate 使用反射加载的。如果您使用反射器查看已编译的 DAL 程序集,则很可能它甚至没有在引用的程序集列表中,因为代码不直接使用它。您已经通过引用单元测试项目找到了解决方案。
Your DAL project references the
System.Data.SQLite
assembly but this doesn't mean it will get copied to the output folder of the test project especially if it is loaded using reflection by NHibernate. Chances are if you look at the compiled DAL assembly with reflector it doesn't even have it in the referenced assemblies list as it is not used directly by the code. You've already found the solution by referencing it to the unit test project.您可以使用“构建后”步骤手动将 dll 复制到输出文件夹。
You could use the Post-build step to copy the dll to your output folder manually.