gpt4 book ai didi

vb.net - 实现 NHibernate 单元测试以使用 VB.NET/MBUnit 生成模式

转载 作者:行者123 更新时间:2023-12-05 01:15:04 24 4
gpt4 key购买 nike

我正在尝试为我的 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(包含 Test Fixture 和 NHibernate 配置)。当我尝试使用 MBUnit GUI 运行此测试时,我收到此消息(第 10 行是对 .Configure 方法的调用):

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>

最佳答案

该部分声明包含类声明和映射文件的程序集的名称。映射文件包含用于将 POCO 类映射到数据库表(或多个表)的元数据。

另一方面,你有

cfg.AddAssembly(GetType(Todd.Core.Product).Assembly)

在您的“GenerateSchema_Fixture”中。这意味着 NHibernate 将加载程序集中包含的所有映射文件。

您必须决定以何种方式为 NHibernate 提供信息。

如果您想以编程方式完成所有操作,我建议您使用 FluentNHibernate。您通过流畅的界面声明所有内容,而不必考虑 XML 文件。 (fluentnhibernate.org)。

关于vb.net - 实现 NHibernate 单元测试以使用 VB.NET/MBUnit 生成模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1070090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com