流畅的NHibernate持久性规范组件和参考测试

时间:2009-11-21 17:27:35

标签: unit-testing nhibernate fluent-nhibernate testing components

我有两个问题。

1。 FNH没有正确测试我的组件,我也不知道为什么。

  

System.ApplicationException:预期   'DomainModel.Model.Publisher'但得到了   'DomainModel.Model.Publisher'代表   财产'出版商'。

[TestMethod]
public void CanCorrectlyMapBook()
{
    new PersistenceSpecification<Book>(_session)
        .CheckProperty(p => p.Name, "My Book")
        .CheckProperty(p=> p.Id, 1)
        .CheckProperty(p=>p.IncludesCDDVD, true)
        .CheckProperty(p=>p.Isbn, "rder93q43949éwr")
        .CheckProperty(p=>p.IsLoaned, false)
        .CheckProperty(p=>p.Publisher, new Publisher(){PublisherHomepage = "www.google.de", PublisherName = "google"})
        .VerifyTheMappings();
}

}

2。 FNH没有正确测试我的参考文献。

  

System.ApplicationException:预期   'DomainModel.Model.Employee'但得到了   'EmployeeProxyd6f94daa37c74be8b5ccccf40c5c23fa'   物业'LoanedBy'。

[TestMethod]
public void CanCorrectlyMapBook()
{
    new PersistenceSpecification<Book>(_session)
        .CheckProperty(p => p.Name, "My Book")
        .CheckProperty(p=> p.Id, 1)
        .CheckProperty(p=>p.IncludesCDDVD, true)
        .CheckProperty(p=>p.Isbn, "rder93q43949éwr")
        .CheckProperty(p=>p.IsLoaned, false)
        .CheckReference(p=>p.LoanedBy, new Employee(){EMail = "",FirstName = "Alex", LastName = "Mueller"})
        .VerifyTheMappings();
}

但是当我“手动”测试时,一切正常。

 ISession mysession = Helper.CreateSessionFactory(false, false).OpenSession();
            Book myBook = new Book()
                              {
                                  Author = "Hesse",
                                  IncludesCDDVD = true,
                                  DateOfIssue = DateTime.Now,
                                  Isbn = "erwe0ri",
                                  IsLoaned = true,
                                  Name = "My Book new",
                                  Publisher = new Publisher() { PublisherHomepage = "www.google.de", PublisherName = "google" },
                                  Release = new Release() { ReleaseDate = DateTime.Now, ReleaseNumber = 1 },
                                  LoanedBy = new Employee() { EMail = "", FirstName = "Alex", LastName = "Mueller" }
                              };

            mysession.Save(myBook);
            mysession.Close();
            mysession.Dispose();

我通过查询数据库来验证这一点......

PersistenceSpecification Tests针对内存数据库sqllite运行,我的手动“test”针对Sql Server 2008运行。

您是否有人使用FNH并正确测试了参考和组件?

1 个答案:

答案 0 :(得分:3)

我认为您需要在相关实体上实现object.Equals()方法,或者在构造PersistenceSpecification时实现IEqualityComparer并将其注入。

例如:

public class A
{
    private int Id { get; set; }
    public virtual B B_Member { get; set; }

    public class Map : ClassMap<A>
    {
        public Map()
        {
            Id(x => x.Id);
            References(x => x.B_Member);
        }
    }
}

public class B
{
    private int Id { get; set; }
    public virtual string BString { get; set; }

    public class Map : ClassMap<B>
    {
        public Map()
        {
            Id(x => x.Id);
            Map(x => x.BString);
        }
    }

    /// remove this method to have the verification fail
    public override bool Equals(object obj)
    {
        var lhs = obj as B;
        if (lhs == null) return false;
        return BString == lhs.BString;
    }
}

    [Test]
    public void Verify()
    {
        var fcfg = Fluently.Configure()
            .Database(SQLiteConfiguration.Standard.UsingFile("testdb.sqldb"))
            .Mappings(mc =>
            {
                mc.FluentMappings.Add(typeof (A.Map));
                mc.FluentMappings.Add(typeof (B.Map));
            })
            .ExposeConfiguration(cfg => new SchemaExport(cfg).Execute(true, true, false));

        var sess = fcfg.BuildSessionFactory().OpenSession();

        new PersistenceSpecification<A>(sess)
            .CheckReference(x => x.B_Member, new B() {BString = "hi"})
            .VerifyTheMappings();

        Assert.Throws<ApplicationException>(
            () => new PersistenceSpecification<A>(sess, new AlwaysFalseEqualityComparer())
                    .CheckReference(x => x.B_Member, new B() {BString = "az"})
                    .VerifyTheMappings());
    }

另请注意,每个属性比较的相关FNH代码是(反射器的补充):

    internal virtual void CheckValue(object target)
{
    bool areEqual;
    object actual = this.property.GetValue(target, null);
    if (this.entityEqualityComparer != null)
    {
        areEqual = this.entityEqualityComparer.Equals(this.propertyValue, actual);
    }
    else
    {
        areEqual = this.propertyValue.Equals(actual);
    }
    if (!areEqual)
    {
        throw new ApplicationException(string.Format("Expected '{0}' but got '{1}' for Property '{2}'", this.propertyValue, actual, this.property.Name));
    }
}

当然,这个异常似乎与您遇到的异常相符。