我目前正在开展一个大学项目,我们正在使用Fluent NHibernate。我正在研究如何为我们的实体和Fluent映射创建测试。
然而,在试图弄清楚如何使用PersistenceSpecification的CheckList时,我已经走到了尽头。单元测试失败,出现以下错误:
MvcShop.Core.Tests.EntitiesTests.ItemTest.CanMapItem threw exception: NHibernate.PropertyValueException: not-null property references a null or transient valueMvcShop.Core.Entities.ItemPicture.Item.
测试定义为:
private IList<ItemPicture> _itemPictures = new List<ItemPicture>()
{
new ItemPicture { Filename = "test.jpg", Title = "Test title", PrimaryPicture = true},
new ItemPicture { Filename = "test2.jpg", Title = "Test title 2" }
};
[TestMethod]
public void CanMapItem()
{
new PersistenceSpecification<Item>(Session)
.CheckProperty(i => i.Title, "Test item")
.CheckProperty(i => i.Description, "Test description")
.CheckProperty(i => i.SalesPrice, (decimal)0.0)
.CheckList(i => i.ItemPictures, _itemPictures) // Complains that Item on ItemPicture is null.
.VerifyTheMappings();
}
我的映射定义为:
public ItemMap()
{
Table("Item");
Id(i => i.ItemID).GeneratedBy.Identity().Column("Item_id");
Map(i => i.ItemNo).Nullable().Length(30);
Map(i => i.Title).Not.Nullable().Length(250);
Map(i => i.Description).Nullable();
Map(i => i.SalesPrice).Not.Nullable().Precision(18);
Map(i => i.AverageRating).Precision(18).Nullable();
Map(i => i.Visible).Not.Nullable();
Map(i => i.Weight).Not.Nullable().Precision(18);
Map(i => i.TimesPurchased);
Map(i => i.InStock).Not.Nullable();
Map(i => i.DateAdded).Not.Nullable();
HasManyToMany(i => i.ItemCategories).Cascade.All().Inverse().Table("ItemCategoryItem");
HasMany(i => i.ItemPictures).Cascade.AllDeleteOrphan().Inverse().LazyLoad();
HasMany(i => i.Comments).Cascade.AllDeleteOrphan().Inverse().LazyLoad();
HasMany(i => i.Ratings).Inverse().LazyLoad();
}
public ItemPictureMap()
{
Table("ItemPicture");
Id(i => i.ItemPictureID).GeneratedBy.Identity().Column("ItemPicture_id");
Map(i => i.Title).Nullable();
Map(i => i.Filename).Not.Nullable();
Map(i => i.PrimaryPicture).Not.Nullable();
References(i => i.Item).Not.Nullable().Column("Item_id");
}
在使用PersistenceSpecification类时,我真的无法想象如何填充ItemPicture的Item属性。
有什么想法吗?
最诚挚的问候, 丹麦肯尼斯
答案 0 :(得分:4)
我认为在对它运行测试之前,ItemPicture必须存在于数据库中(根据Fluent文档:http://wiki.fluentnhibernate.org/Persistence_specification_testing - 请参见该页面的最后一行。)
尝试:
[TestMethod]
public void CanMapItem()
{
var p1 = new ItemPicture { Filename = "test.jpg", Title = "Test title", PrimaryPicture = true};
var p2 = new ItemPicture { Filename = "test2.jpg", Title = "Test title 2" };
using (var tx = Session.BeginTransaction())
{
Session.Save(p1);
Session.Save(p2);
};
new PersistenceSpecification<Item>(Session)
.CheckProperty(i => i.Title, "Test item")
.CheckProperty(i => i.Description, "Test description")
.CheckProperty(i => i.SalesPrice, (decimal)0.0)
.CheckList(i => i.ItemPictures, new List<ItemPicture> {p1, p2});
.VerifyTheMappings();
}
答案 1 :(得分:2)
CheckList似乎有一个bug。虽然你的映射是正确的,它会在坚持父母之前坚持孩子。 如果您改为使用CheckComponentList,那么您的Item将在ItemPictures之前被激活,并且您的测试应该通过。
答案 2 :(得分:1)
您不能需要覆盖GetHashCode和Equals并创建自己的IEqualityComparer。这样做的原因是默认情况下不能比较实体,而值对象可以。
默认情况下,无法比较DateTime实例,原因很简单,因为它们不是值,它们是具有值的实例,而datetime1!= datetime2即使它们的日期完全相同,所以您需要做的是比较它们的关键值。对于像上面这样的课程。我认为Item包含一堆ItemPictures,然后在你的IEqualityComparer实现中,当你重写equals时,你应该检查当前对象是否是ItemPicture类型,以及是否检查当前ItemPicture的id是否与其他边ItemPicture.Id匹配。由于代码比单词更响亮,我会给你一个小例子:
[TestMethod]
public void CanMapItem()
{
new PersistenceSpecification<Item>(Session, new CustomIEqualityComparer())
.CheckProperty(i => i.Title, "Test item")
.CheckProperty(i => i.Description, "Test description")
.CheckProperty(i => i.SalesPrice, (decimal)0.0)
.CheckList(i => i.ItemPictures, _itemPictures) // Complains that Item on ItemPicture is null.
.VerifyTheMappings();
}
public class CustomIEqualityComparer: IEqualityComparer
{
public bool Equals(object x, object y)
{
if (x == null || y == null)
{
return false;
}
if (x is ItemPicture && y is ItemPicture)
{
return ((ItemPicture) x).Id == ((ItemPicture) y).Id;
}
if(x is DateTime && y is DateTime)
{
return ((DateTime)x).Year ==((DateTime)y).Year;
}
return x.Equals(y);
}
public int GetHashCode(object obj)
{
throw new NotImplementedException();
}
}