本文介绍了流利的NHibernate PersistenceSpecification组件和参考测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题。

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



  [TestMethod] 
public void CanCorrectlyMapBook()
{
new PersistenceSpecification< Book>(_ session)
.CheckProperty(p => p.Name, (p => p.IncludesCDDVD,true)
.CheckProperty(p => (p = Ip.Isbn,rder93q43949ewr)
.CheckProperty(p => p.IsLoaned,false)
.CheckProperty(p => p.Publisher,new Publisher(){PublisherHomepage =www。 google.de,PublisherName =google})
.VerifyTheMappings();

code










$ b $ p


$ b

2。
$ b



  [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)$ b $ 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测试是针对内存数据库sqllite运行的,而我的手动test是针对Sql Server 2008运行的。



有没有人已经使用了FNH,并正确地测试了一个引用和一个组件?解决方案

我认为你需要实现object.Equals()方法,或者实现一个IEqualityComparer,并在构造PersistenceSpecification时注入它。



例如:

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

public class Map:ClassMap< A>
{
public Map()
{
Id(x => x.Id);
参考文献(x => x.B_Member);




public class B
{
private int Id {get;组; }
公共虚拟字符串BString {get;组; }

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



///删除此方法使验证失败
public override bool Equals(object obj)
{
var lhs = obj为B;
if(lhs == null)return false;
return BString == lhs.BString;


$ b $测试
public void Verify()
{
var fcfg = Fluently.Configure()
。数据库(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();

新的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());





$ b注意每个属性比较的相关FNH代码是(反射器的补码):
$ b $ pre $ 内部虚拟无效CheckValue(对象目标)
{
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)
{
抛出新的ApplicationException(string.Format(Expected {0}),但得到了'{1}'属性'{2 }',this.propertyValue,actual,this.property.Name));


$ / code $ / pre

当然,这个异常似乎与你所体验。

I have two problems.

1 . FNH does not test my component correcty and I dont know why.

[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 does not test my reference correctly.

[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();
}

But when I test this "manually" everything works fine.

 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();

I have verify this by looking up in the datbase ...

The PersistenceSpecification Tests run against an in-memory database sqllite and my manual "test" runs against a Sql Server 2008.

Does anybody of you have used FNH and tested a reference and a component correctly?

解决方案

I think you either need to implement the object.Equals() method on the relevant entities, or implement an IEqualityComparer and inject it when you construct the PersistenceSpecification.

For example:

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());
    }

Note also that the relevant FNH code for each property comparison is (compliments of reflector):

    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));
    }
}

Certainly that that exception seems to match the ones you are experiencing.

这篇关于流利的NHibernate PersistenceSpecification组件和参考测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:00