本文介绍了Fluent Nhibernate映射的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解社区的其他人如何测试他们的流畅Nhibernate映射。所以我们可以说我有以下映射:
$ b $ pre $ public $ User $
{
表(USER_HEADER );
Id(x => x.Id,USER_ID);
Map(x => x.LoginName,LOGIN_NAME);
Map(x => x.UserPassword,USER_PASSWORD);
Map(x => x.UserEmail,USER_EMAIL);
Map(x => x.UserLanguage,USER_LANGUAGE);
Map(x => x.UserEnabled,USER_ENABLED);

HasManyToMany(X => x.Groups)
。表( USER_GROUP_COMPOSITE)
.ParentKeyColumn( USER_ID)
.ChildKeyColumn( GROUP_ID )
.Cascade.All()
.Inverse();
}

public class GroupHeaderMap:ClassMap< GroupHeader>
{
public GroupHeaderMap()
{
表(GROUP_HEADER);
Id(x => x.Id,GROUP_ID);
地图(x => x.Name,GROUP_NAME);
Map(x => x.Description,GROUP_DESCRIPTION);

HasManyToMany(X => x.Users)
。表( USER_GROUP_COMPOSITE)
.ParentKeyColumn( GROUP_ID)
.ChildKeyColumn( USER_ID );




$ b $ p
$ b $ p你会为这些单元测试写什么?你会使用PersistenceSpecification类来单元测试这些?

编辑:



我想使用SqlLite,但如果我不从我的映射生成我的模式?我仍然可以以某种方式将我的模式加载到SqlLite中吗?另外我想知道如果测试SqlLite是真的够了。我们的产品至少需要运行MS SQL和Oracle。仅在SqlLite数据库上进行测试是否符合我的要求?你也通常测试你已经映射的每个实体(构造函数,属性等)吗?

解决方案

流利的nhibernate有内置测试方法。有了它们,你可以做到以下几点:$ b​​
$ b pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ {
新PersistenceSpecification<员工>(会话)
.CheckProperty(C => c.Id,1)
.CheckProperty(C => c.FirstName, 约翰)
.CheckProperty(c => c.LastName,Doe)
.VerifyTheMappings();



$ b $ p
$ b $ p

ul>

  • 创建一个Employee实例

  • 将Employee插入数据库
  • 将记录检索到新的Employee实例

  • 验证检索到的Employee是否与原始的


    匹配。这将检查属性映射。另外我建议使用SqlLite并在内存中测试真正的SQL查询以验证级联规则。

    I'm trying to get a feel for how the rest of the community tests their Fluent Nhibernate mappings. So let's say I have the following mappings:

        public UserHeaderMap()
        {
            Table("USER_HEADER");
            Id(x => x.Id, "USER_ID");
            Map(x => x.LoginName, "LOGIN_NAME");
            Map(x => x.UserPassword, "USER_PASSWORD");
            Map(x => x.UserEmail, "USER_EMAIL");
            Map(x => x.UserLanguage, "USER_LANGUAGE");
            Map(x => x.UserEnabled, "USER_ENABLED");
    
            HasManyToMany(x => x.Groups)
                .Table("USER_GROUP_COMPOSITE")
                .ParentKeyColumn("USER_ID")
                .ChildKeyColumn("GROUP_ID")
                .Cascade.All()
                .Inverse();
        }
    
    public class GroupHeaderMap : ClassMap<GroupHeader>
    {
        public GroupHeaderMap()
        {
            Table("GROUP_HEADER");
            Id(x => x.Id, "GROUP_ID");
            Map(x => x.Name, "GROUP_NAME");
            Map(x => x.Description, "GROUP_DESCRIPTION");
    
            HasManyToMany(x => x.Users)
                .Table("USER_GROUP_COMPOSITE")
                .ParentKeyColumn("GROUP_ID")
                .ChildKeyColumn("USER_ID");
        }
    }
    

    What all unit tests would you write for these? Would you use PersistenceSpecification class to unit test these?

    Edit:

    I want to use SqlLite but what if I'm not generating my schema from my mappings? Can I still load my schema into SqlLite somehow? Also I'm wondering if testing SqlLite is really enough. Our product needs to run on at least MS SQL and Oracle. Will testing only on a SqlLite database meet my requirements? Also do you usually test each and every entity that you have mapped (Constructors, Properties, etc)?

    解决方案

    Fluent nhibernate has build-in testing methods. With them you can do the following

    [Test]
    public void CanCorrectlyMapEmployee()
    {
        new PersistenceSpecification<Employee>(session)
            .CheckProperty(c => c.Id, 1)
            .CheckProperty(c => c.FirstName, "John")
            .CheckProperty(c => c.LastName, "Doe")
            .VerifyTheMappings();
    }
    

    This test will

    • create an Employee instance
    • insert the Employee into the database
    • retrieve the record into a new Employee instance
    • verify the retrieved Employee matches the original

    This will check property mappings.

    Also I would suggest to use SqlLite and test real SQL queries in memory to verify cascading rules.

    这篇关于Fluent Nhibernate映射的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-19 10:32