简体   繁体   English

NHibernate:如何从单元测试项目中引用主项目的装配?

[英]NHibernate: How to reference the assembly of the main project from the unit test project?

I built a simple NHibernate Application but i wanted to use Mapping Attributes instead of Mapping XML. 我构建了一个简单的NHibernate应用程序,但是我想使用Mapping Attributes而不是Mapping XML。

Now i have the problem, when i create a Unit Test Project and want to load the Mapping Attributes, that my domain objects are in the other project! 现在,当我创建一个单元测试项目并想加载映射属性时,我遇到了问题,即我的域对象在另一个项目中!

This is my Unit Test: 这是我的单元测试:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace Tests
{
[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        Configuration cfg = new Configuration();
        cfg.Configure();
        cfg.AddInputStream(NHibernate.Mapping.Attributes.HbmSerializer.Default.Serialize(
            System.Reflection.Assembly.GetExecutingAssembly()));
        new SchemaExport(cfg).Execute(false, true, false);
    }
}

In this line 在这条线

cfg.AddInputStream(NHibernate.Mapping.Attributes.HbmSerializer.Default.Serialize(
            System.Reflection.Assembly.GetExecutingAssembly()));

I get this Exception: 我得到这个异常:

Result Message: 
Test method Tests.UnitTest1.TestMethod1 threw exception: 
NHibernate.Mapping.Attributes.MappingException: The following assembly contains 
no mapped classes: Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Result StackTrace:  
at NHibernate.Mapping.Attributes.HbmSerializer.Serialize(Stream stream, Assembly assembly)
   at NHibernate.Mapping.Attributes.HbmSerializer.Serialize(Assembly assembly)
   at Tests.UnitTest1.TestMethod1() in ...Tests\UnitTest1.cs:Line 17.

I guess instead of 我想代替

System.Reflection.Assembly.GetExecutingAssembly()

I have to get somehow the assembly of my main project. 我必须以某种方式获得我的主要项目的组装。

I found the easiest way to do this was with: 我发现最简单的方法是:

Assembly.Load("Project Name of Desired Assembly")

I read a lot of stuff online to do this, including this post, and nothing was more simple than using this method. 我在网上阅读了很多相关内容,包括这篇文章,没有什么比使用这种方法更简单的了。 Also, the methods mentioned by JoelC probably work, however I couldn't get them to work for me and this worked no problems. 另外,JoelC提到的方法可能有效,但是我无法让它们为我工作,这没有问题。

Yeah, calling System.Reflection.Assembly.GetExecutingAssembly() from unit tests usually will give you the unit test runner as the executing assembly. 是的,通常从单元测试中调用System.Reflection.Assembly.GetExecutingAssembly()会将单元测试运行器作为执行程序集。

You should be able to call typeof([TargetAssemblyClass]).Assembly to find your main project assembly. 您应该能够调用typeof([TargetAssemblyClass]).Assembly来找到您的主项目程序集。 This, of course, requires a reference to the main project which I expect you already have. 当然,这需要参考我希望您已经拥有的主要项目。

Edit: Calling System.Reflection.Assembly.GetAssembly(typeof([TargetAssemblyClass])) is another way to achieve the same thing. 编辑:调用System.Reflection.Assembly.GetAssembly(typeof([TargetAssemblyClass]))是实现同一件事的另一种方法。

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

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