0

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] 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")

}

4

2 に答える 2

1

IdプロパティはデータベースIDであるため、テーブルに挿入するたびに増分されます。他のテストでもUserProfileが挿入されているため、この挿入ではID値が2にインクリメントされます。Idプロパティが0に等しくないことを確認します。これがデフォルト値であると仮定します。

于 2010-07-06T16:51:08.033 に答える
0

これらのテストをマッピングのみに分離できるように、メモリ内データベースを使用してマッピングをテストすることをお勧めします。メモリ内データベースを使用する場合、FluentConfiguration を [TestInitialize] (MSTest) または [SetUp] (NUnit) メソッドに配置すると、データベースは毎回最初から (メモリ内に) 作成されます。次に例を示します。

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

このシナリオでは、System.Data.SQLite DLL と共に SQLite を使用する必要があります。この DLL はhttp://sqlite.phxsoftware.com/にあります。

それが役立つことを願っています。

于 2010-07-08T14:11:34.217 に答える