开发者

Is this Fluent NHibernate mapping test a false positive?

开发者 https://www.devze.com 2023-03-16 08:40 出处:网络
I changed my mapping test to use the overload .VerifyTheMappings(TEntity first), and suddenly my test just passes. I haven\'t used that overload before, and since I\'m not really sure how it works, I\

I changed my mapping test to use the overload .VerifyTheMappings(TEntity first), and suddenly my test just passes. I haven't used that overload before, and since I'm not really sure how it works, I'm worried I'm getting a false positive.

I'm surprised mainly because I didn't think I was using auto mapping, and I haven't even mapped up the entire entity yet. So now I'm wondering:

Does this test pass because It should, or because it doesn't really test anything?

Update: After further investigating I've found that as long as I map the ID, everything works. Is this how it's supposed to be? What does .VerifyTheMappings(TEntity first) really test?

My code:

Configuration

 Fluently.Configure()
     .Database(MsSqlConfiguration.MsSql2008.DefaultSchema("dbo")
     .ConnectionString(ConfigurationManager.ConnectionStrings["Nitro_Empty"].ConnectionString))
     .Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserMap>());

The entity:

public class InspectionObject : IEntity<int>
{
    public virtual int ID { get; set; }

    public virtual Project Project { get; set; }
    public virtual InspectionObjectType Type { get; set; }

    public virtual User CreatedByUser { get; set; }
    public virtual DateTime Created { get; set; }
    public virtual DateTime LastUpdated { get; set; }
    public virtual User LastUpdatedByUser { get; set; }

    public virtual string Littera { get; set; }
    public virtual string Owner { get; set; }
    public virtual string Address { get; set; }
    public virtual string Caretaker { get; set; }
    public virtual string Remarks { get; set; }
    public virtual float PlacementX { get; set; }
    public virtual float PlacementY { get; set; }
    public virtual float PlacementZ { get; set; }
}

The map:

public class InspectionObjectMap : ClassMap<InspectionObject>
{
    public InspectionObjectMap()
    {
        Id(i => i.ID).Not.Nullable();
    }
}

The test:

    [Test]
    public void IspectionObject_MappingsAreOK()
    {
        var dtenow = DateTime.Now;
        dtenow = new DateTime(dtenow.Ticks - (dtenow.Ticks % TimeSpan.TicksPerSecond), dtenow.Kind);

        var io = new InspectionObject
        {
            ID = 1,
            Project = Data.Project,
            Type = Data.InspectionObjectTypeVilla,
            CreatedByUser = Data.Consultant,
            Created = dtenow,
            LastUpdatedByUser = Data.UserConsultant1,
            LastUpdated = dtenow.AddHours(1),

            Littera = "15",
            Owner = "Fastighetsägare",
            Address = "Adress",
            Caretaker = "Fastighetsskötare",
            Remarks = "Anteckningar",

            PlacementX = 1F,
            PlacementY = 2F,
            PlacementZ = 3F
        };

        var session = IoC.Resolve<ISession>();

        new Pe开发者_如何转开发rsistenceSpecification<InspectionObject>.VerifyTheMappings(io);
    }

(yes, I have some export/drop schema in my SetUp and TearDown routines as well, but it's all standard. It's just a little too deeply nested into the testing framework built around this application for me to want to dig out the relevant pieces, since it's not doing anything fancy, just in fancy places...)


I've found that as long as I map the ID, everything works. Is this how it's supposed to be? What does .VerifyTheMappings(TEntity first) really test?

You can easily see how the test works by looking at it's source

All VerifyTheMappings appears to be testing is whether it can commit and load your object (as such it only needs to have an ID mapped). If you want to test anything other than whether you can save or load an ID, you have to get some values into the allProperties List in that object. This is commonly done with the CheckProperty or CheckReference calls. VerifyTheMappings(T first) doesn't do anything special to populate allProperties automatically. The documentation provides the following example of correct use:

public void CanCorrectlyMapEmployee()
{
    new PersistenceSpecification<Employee>(session, new CustomEqualityComparer())
        .CheckProperty(c => c.Id, 1)
        .CheckProperty(c => c.FirstName, "John")
        .CheckProperty(c => c.LastName, "Doe")
        .CheckReference(c => c.Store, new Store() {Name = "MyStore"})
        .VerifyTheMappings();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消