0

두 가지 문제점이 있습니다.Fluent NHibernate PersistenceSpecification 구성 요소 및 참조 테스트

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

그러나 나는이 "수동"모든 것이 잘 작동 테스트 할 때. 나는 DATBASE에서 보면이를 확인이

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에 대해 실행하고 내 매뉴얼 "시험"으로 SQL Server에 대해 실행 2008

FNH를 사용하고 참조 및 구성 요소를 올바르게 테스트 한 사람이 있습니까?

답변

3

관련 엔터티에 object.Equals() 메서드를 구현하거나 IEqualityComparer를 구현하고 PersistenceSpecification을 생성 할 때 삽입해야한다고 생각합니다. 예를 들어

:

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

경험.

+0

나는 이것이 도움이되지 않는다고 생각한다. 구성 요소는 엔티티가 아니기 때문에 아무런 이슈가 없기 때문이다. object.Equals 메서드를 구현했지만 내 구성 요소 및 참조에 대해 여전히 동일한 오류 메시지가 있습니다. 그러나 누군가가 FNH API가 올바르게 작동하는 것을 볼 수있는 간단한 예제를 보여줄 수 있다면 좋을 것입니다. 나를 위해 그것이하지 않는 것 같습니다. – Rookian

+0

엔티티가 아닌 요소가 equals 또는 equalitycomparer를 구현하면 도움이되지 않는다는 것을 의미하는 이유가 확실하지 않습니다. 나는 참조 용으로 간단한 예제를 제공했다. – fostandy

관련 문제