我試圖爲我的NHibernate數據訪問層實現單元測試。我從網上找到的一個例子(http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/01/your-first-nhibernate-based-application.aspx)中抽取的第一個測試只是試圖使用我的域類/映射重新創建數據庫。我已經能夠在C#中使用這個示例(產品表是在數據庫中創建的),但是不能在VB.NET中實現時使用。我有Todd.Core(包含Product類和Product.hbm.xml映射)和Todd.Core.Test(包含測試夾具和NHibernate配置)兩個項目。當我嘗試使用MbUnit的GUI來運行該測試我收到此消息(第10行是在調用.Configure法):實現NHibernate的單元測試生成與VB.NET/MBUnit架構
Message: Could not compile the mapping document: Todd.Core.Product.hbm.xml
Type: NHibernate.MappingException
Source: NHibernate
TargetSite: Void LogAndThrow(System.Exception)
HelpLink: null
Stack: at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
at NHibernate.Cfg.Configuration.ProcessMappingsQueue()
at NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document)
at NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name)
at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
at NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly)
at NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly)
at NHibernate.Cfg.Configuration.AddAssembly(String assemblyName)
at NHibernate.Cfg.Configuration.DoConfigure(IHibernateConfiguration hc)
at NHibernate.Cfg.Configuration.Configure()
at Todd.Core.Test.GenerateSchema_Fixture.Can_generate_schema() in C:\Development\Todd\Todd.Core.Test\GenerateSchema_Fixture.vb:line 10
理解任何想法。以下是我的代碼...。
我的產品類:
Namespace Todd.Core
Public Class Product
Private _id As Guid
Private _name As String
Private _category As String
Private _discontinued As Boolean
Public Overridable Property Id() As Guid
Get
Return _id
End Get
Set(ByVal value As Guid)
_id = value
End Set
End Property
Public Overridable Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Overridable Property Category() As String
Get
Return _category
End Get
Set(ByVal value As String)
_category = value
End Set
End Property
Public Overridable Property Discontinued() As Boolean
Get
Return _discontinued
End Get
Set(ByVal value As Boolean)
_discontinued = value
End Set
End Property
End Class
End Namespace
我Product.hbm.xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Todd.Core.Product, Todd.Core" table="Product">
<id name="Id">
<generator class="guid" />
</id>
<property name="Name" />
<property name="Category" />
<property name="Discontinued" />
</class>
</hibernate-mapping>
我的測試夾具:
Imports MbUnit.Framework
Imports Todd.Core
<TestFixture()> _
Public Class GenerateSchema_Fixture
<Test()> _
Public Sub Can_generate_schema()
Dim cfg As New NHibernate.Cfg.Configuration
cfg.Configure()
cfg.AddAssembly(GetType(Todd.Core.Product).Assembly)
Dim exp As NHibernate.Tool.hbm2ddl.SchemaExport = New NHibernate.Tool.hbm2ddl.SchemaExport(cfg)
exp.Execute(True, True, False, True)
End Sub
End Class
我的app.config(從測試項目) :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property>
<property name="connection.connection_string">data source=.\SQLEXPRESS;Initial Catalog=NHibernateTestDB;Integrated Security=SSPI</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="show_sql">true</property>
<mapping assembly="Todd.Core" />
</session-factory>
</hibernate-configuration>
</configuration>
請格式化您的代碼 – 2009-07-01 16:34:55