Fluent NHibernate PersistenceSpecification 无法测试字符串集合
我正在使用 Fluent NHibernate 映射一个具有如下字符串集合的类:
public class Foo {
public virtual ICollection<string> Strings { get; set; }
}
public class FooMap : ClassMap<Foo> {
public FooMap() { HasMany(f => f.Strings).Element("SomeColumnName"); }
}
当我使用 FNH 包中包含的 PersistenceSpecification
类编写单元测试时,它失败:
[TestMethod]
public void CanMapCollectionOfStrings() {
var someStrings = new List<string> { "Foo", "Bar", "Baz" };
new PersistenceSpecification<Foo>(CurrentSession)
.CheckList(x => x.Strings, someStrings) // MappingException
.VerifyTheMappings();
}
此测试抛出 NHibernate.MappingException:调用
。但是,如果我尝试自己保留该对象,它就可以正常工作。CheckList()
时没有持久化:System.String
[TestMethod]
public void CanPersistCollectionOfStrings() {
var foo = new Foo {
Strings = new List<string> { "Foo", "Bar", "Baz" };
};
CurrentSession.Save(foo);
CurrentSession.Flush();
var savedFoo = CurrentSession.Linq<Foo>.First();
Assert.AreEqual(3, savedFoo.Strings.Count());
// Test passes
}
为什么第一个单元测试失败?
I'm using Fluent NHibernate to map a a class that has a collection of strings like this:
public class Foo {
public virtual ICollection<string> Strings { get; set; }
}
public class FooMap : ClassMap<Foo> {
public FooMap() { HasMany(f => f.Strings).Element("SomeColumnName"); }
}
When I write a unit test using the PersistenceSpecification
class included in the FNH package, it fails:
[TestMethod]
public void CanMapCollectionOfStrings() {
var someStrings = new List<string> { "Foo", "Bar", "Baz" };
new PersistenceSpecification<Foo>(CurrentSession)
.CheckList(x => x.Strings, someStrings) // MappingException
.VerifyTheMappings();
}
This test throws NHibernate.MappingException: No persister for: System.String
when calling CheckList()
. However, if I try to persist the object myself, it works just fine.
[TestMethod]
public void CanPersistCollectionOfStrings() {
var foo = new Foo {
Strings = new List<string> { "Foo", "Bar", "Baz" };
};
CurrentSession.Save(foo);
CurrentSession.Flush();
var savedFoo = CurrentSession.Linq<Foo>.First();
Assert.AreEqual(3, savedFoo.Strings.Count());
// Test passes
}
Why is the first unit test failing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,CheckComponentList 方法可能是正确的方法:
此代码对我来说效果很好(NH 3.1,FNH 1.2)。希望这有帮助。
CheckComponentList method is probably the right way in this case:
This code work well for me (NH 3.1, FNH 1.2). Hope this help.