Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent NHibernate PersistenceSpecification can't test a collection of strings

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?

like image 754
Brant Bobby Avatar asked Sep 24 '10 18:09

Brant Bobby


1 Answers

CheckComponentList method is probably the right way in this case:

var someStrings = new List<string> { "Foo", "Bar", "Baz" };
new PersistenceSpecification<Foo>(CurrentSession)
    .CheckComponentList(x => x.Strings, someStrings)
    .VerifyTheMappings();

This code work well for me (NH 3.1, FNH 1.2). Hope this help.

like image 104
Jakub Linhart Avatar answered Sep 28 '22 02:09

Jakub Linhart