0

嗨,我正在为流利的 Nhibernate 编写单元测试,当我在隔离中运行测试时它通过了,但是当我运行多个测试时。或多次运行测试,它开始失败并显示 System.ApplicationException 下面的消息:对于属性“Id”预期“1”类型为“System.Int32”但得到“2”类型为“System.Int32”

[TextFixture] 公共无效 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")

}

4

2 回答 2

1

Id 属性是一个数据库标识,因此每次插入表时都会递增。其他一些测试也插入了一个 UserProfile,因此此插入的标识值增加到 2。我只是验证 Id 属性不等于 0,假设这是它的默认值。

于 2010-07-06T16:51:08.033 回答
0

我建议使用内存数据库测试您的映射,以便您可以仅将这些测试与映射隔离。如果使用内存数据库,可以将 FluentConfiguration 放在 [TestInitialize] (MSTest) 或 [SetUp] (NUnit) 方法中,每次都会从头开始(在内存中)创建 db。这是一个例子:

[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);  
}  

然后你的测试应该在每次运行时都能正常工作:

[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,您可以在此处找到:http: //sqlite.phxsoftware.com/

希望有帮助。

于 2010-07-08T14:11:34.217 回答