Fleunt NHibernate 无法在 nunit 测试装置之外工作
好的,这是我的问题...
我使用 RTM Fluent Nhibernate 创建了一个数据层。我的创建会话代码如下所示:
_session = Fluently.Configure().
Database(SQLiteConfiguration.Standard.UsingFile("Data.s3db"))
.Mappings( m =>
{
m.FluentMappings.AddFromAssemblyOf<ProductMap>();
m.FluentMappings.AddFromAssemblyOf<ProductLogMap>();
})
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
当我在测试项目中引用该模块时,然后创建一个如下所示的测试装置:
[Test]
public void CanAddProduct()
{
var product = new Product {Code = "9", Name = "Test 9"};
IProductRepository repository = new ProductRepository();
repository.AddProduct(product);
using (ISession session = OrmHelper.OpenSession())
{
var fromDb = session.Get<Product>(product.Id);
Assert.IsNotNull(fromDb);
Assert.AreNotSame(fromDb, product);
Assert.AreEqual(fromDb.Id, product.Id);
}
我的测试通过了。当我打开创建的 SQLite DB 时,代码为 9 的新产品就在其中。 Product 和 ProductLog 的表就在那里。
现在,当我创建一个新的控制台应用程序并引用相同的库时,请执行以下操作:
Product product = new Product() {Code = "10", Name = "Hello"};
IProductRepository repository = new ProductRepository();
repository.AddProduct(product);
Console.WriteLine(product.Id);
Console.ReadLine();
它不起作用。我实际上得到了非常讨厌的异常链。为了让您省去很多麻烦,这里是摘要:
顶级异常:
创建 SessionFactory 时使用了无效或不完整的配置。检查 PotentialReasons 集合和 InnerException 以获取更多详细信息。\r\n\r\n
PotentialReasons 集合为空
内部异常:
无法在程序集 System.Data.SQLite 中实现 IDbCommand 和 IDbConnection成立。确保程序集 System.Data.SQLite 位于应用程序目录或全局程序集缓存中。如果程序集位于 GAC 中,请使用应用程序配置文件中的 element 指定程序集的全名。
单元测试库和控制台应用程序都引用完全相同版本的 System.Data.SQLite。两个项目的调试文件夹中都有完全相同的 DLL。我什至尝试将创建的单元测试库 SQLite DB 复制到控制台应用程序的调试目录中,并删除构建架构行,但它仍然失败
如果有人可以帮助我弄清楚为什么这在我的单元测试之外不起作用它会不胜感激。这个疯狂的错误让我停滞不前。
Okay, here is my problem...
I created a Data Layer using the RTM Fluent Nhibernate. My create session code looks like this:
_session = Fluently.Configure().
Database(SQLiteConfiguration.Standard.UsingFile("Data.s3db"))
.Mappings( m =>
{
m.FluentMappings.AddFromAssemblyOf<ProductMap>();
m.FluentMappings.AddFromAssemblyOf<ProductLogMap>();
})
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
When I reference the module in a test project, then create a test fixture that looks something like this:
[Test]
public void CanAddProduct()
{
var product = new Product {Code = "9", Name = "Test 9"};
IProductRepository repository = new ProductRepository();
repository.AddProduct(product);
using (ISession session = OrmHelper.OpenSession())
{
var fromDb = session.Get<Product>(product.Id);
Assert.IsNotNull(fromDb);
Assert.AreNotSame(fromDb, product);
Assert.AreEqual(fromDb.Id, product.Id);
}
My tests pass. When I open up the created SQLite DB, the new Product with Code 9 is in it. the tables for Product and ProductLog are there.
Now, when I create a new console application, and reference the same library, do something like this:
Product product = new Product() {Code = "10", Name = "Hello"};
IProductRepository repository = new ProductRepository();
repository.AddProduct(product);
Console.WriteLine(product.Id);
Console.ReadLine();
It doesn't work. I actually get pretty nasty exception chain. To save you lots of head aches, here is the summary:
Top Level exception:
An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.\r\n\r\n
The PotentialReasons collection is empty
The Inner exception:
The IDbCommand and IDbConnection implementation in the assembly System.Data.SQLite could not be found. Ensure that the assembly System.Data.SQLite is located in the application directory or in the Global Assembly Cache. If the assembly is in the GAC, use element in the application configuration file to specify the full name of the assembly.
Both the unit test library and the console application reference the exact same version of System.Data.SQLite. Both projects have the exact same DLLs in the debug folder. I even tried copying SQLite DB the unit test library created into the debug directory of the console app, and removed the build schema lines and it still fails
If anyone can help me figure out why this won't work outside of my unit tests it would be greatly appreciated. This crazy bug has me at a stand still.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用的是 64x 操作系统,请尝试将编译器构建选项设置为 x86。
If you are on a 64x OS, try setting the compiler build option to x86.