[英]How to test fluent-NHibernate's PersistenceSpecification.VerifyTheMappings with lists and relational objects?
你會如何測試這種情況?
我剛剛開始研究NHibernate並在TDD上進行了第一次打擊。 到目前為止,我真的很喜歡它,並且一直使用流暢的Nhibernate來映射類。
但是,當在PersistenceSpecification上使用VerifyTheMappings方法時,我似乎正在走向死胡同。
基本上我有兩個類,Recipient和RecipientList。 RecipientList類具有到收件人的映射,具有流暢的“HasMany”關系:
public class RecipientListMap : ClassMap<RecipientList>
{
public RecipientListMap()
{
Id(x => x.ID);
Map(x => x.ApplicationID);
Map(x => x.Name);
Map(x => x.IsDeleted);
HasMany<Recipient>(x => x.Recipients).WithKeyColumn("RecipientListID").AsList().LazyLoad();
}
}
但是,當我在測試中使用以下代碼時:
private IList<Recipient> _recipients = new List<Recipient>()
{
new Recipient { FirstName = "Joe", LastName = "Bloggs", Email = "joe@bloggs.com", IsDeleted = false },
new Recipient { FirstName = "John", LastName = "Doe", Email = "john@doe.com", IsDeleted = false },
new Recipient { FirstName = "Jane", LastName = "Smith", Email = "john@smith.com", IsDeleted = false }
};
[Test]
public void Can_Add_RecipientList_To_Database()
{
new PersistenceSpecification<RecipientList>(Session)
.CheckProperty(x => x.Name, "My List")
.CheckProperty(x => x.Columns, "My columns")
.CheckProperty(x => x.IsDeleted, false)
.CheckProperty(x => x.ApplicationID, Guid.NewGuid())
.CheckProperty(x => x.Recipients, _recipients)
.VerifyTheMappings();
}
發生以下錯誤:
failed: System.ApplicationException : Expected 'System.Collections.Generic.List`1[Project.Data.Domains.Recipients.Recipient]' but got 'NHibernate.Collection.Generic.PersistentGenericBag`1[Project.Data.Domains.Recipients.Recipient]' for Property 'Recipients'
我可以看到錯誤是因為我傳入List並且返回的列表是PersistentGenericBag,因此拋出錯誤。 如果你不能只是傳入一個IList,我不會得到你如何測試這個?
任何幫助,將不勝感激。
愚蠢的是我在PeristenceSpecification上使用了錯誤的方法。
我本來應該使用CheckList而不是CheckProperty。
咄!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.