流利的Nhibernate System.ApplicationException:对于属性"Id",预期类型为"System.Int32"的"1",但得到类型为"System.Int32"的"2" [英] Fluent Nhibernate System.ApplicationException : For property 'Id' expected '1' of type 'System.Int32' but got '2' of type 'System.Int32'
问题描述
您好,我正在为流利的Nhibernate编写单元测试,当我在isloation中运行测试时,它通过了,但是当我运行多个测试时,则通过了.或多次运行测试失败,并显示以下消息 System.ApplicationException:对于属性"Id",预期类型为"System.Int32"的"1",但得到类型为"System.Int32"的"2"
Hi I am writing unit tests for fluent Nhibernate, when I run the test in isloation it passes, but when I run multiple tests. or run the test more than once it starts failing with the message below System.ApplicationException : For property 'Id' expected '1' of type 'System.Int32' but got '2' of type 'System.Int32'
[TextFixture] 公共无效Can_Correctly_Map_Entity() {
[TextFixture] public void Can_Correctly_Map_Entity() {
new PersistenceSpecification<UserProfile>(Session)
.CheckProperty(c => c.Id, 1)
.CheckProperty(c => c.UserName, "user")
.CheckProperty(c => c.Address1, "Address1")
.CheckProperty(c => c.Address2, "Address2")
}
推荐答案
我建议使用内存数据库测试映射,以便您可以仅将这些测试隔离在映射中.如果使用内存数据库,则可以将FluentConfiguration放在[TestInitialize](MSTest)或[SetUp](NUnit)方法中,并且每次都会从头开始在内存中创建数据库.这是一个示例:
I'd suggest testing your mappings using an in-memory database so that you can isolate these tests the mappings only. If you use an in-memory database, you can place the FluentConfiguration in the [TestInitialize] (MSTest) or [SetUp] (NUnit) method, and the db will be created from scratch (in memory) each time. Here's an example:
[TestInitialize]
public void PersistenceSpecificationTest()
{
var cfg = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory().UseReflectionOptimizer())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserProfile>())
.BuildConfiguration();
_session = cfg.BuildSessionFactory().OpenSession();
new SchemaExport(cfg).Execute(false, true, false, _session.Connection, null);
}
然后您的测试应该在每次运行时都能正常运行
Then your test should work fine each time you run:
[TestMethod]
public void CanMapUserProfile()
{
new PersistenceSpecification<UserProfile>(_session)
.CheckProperty(c => c.Id, 1)
.CheckProperty(c => c.UserName, "user")
.CheckProperty(c => c.Address1, "Address1")
.CheckProperty(c => c.Address2, "Address2")
}
在这种情况下,您需要使用SQLite以及System.Data.SQLite DLL,可以在这里找到:
You'll need to use SQLite in this scenario, along with the System.Data.SQLite DLL, which you can find here: http://sqlite.phxsoftware.com/
希望有帮助.
这篇关于流利的Nhibernate System.ApplicationException:对于属性"Id",预期类型为"System.Int32"的"1",但得到类型为"System.Int32"的"2"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!