gpt4 book ai didi

unit-testing - 无法将 'NHibernate.Collection.Generic.PersistentGenericSet` 1 转换为 System.Collections.Generic.IList`1

转载 作者:行者123 更新时间:2023-12-04 10:53:04 26 4
gpt4 key购买 nike

我有一个域类:

public class Agencia : IEntity
{
public virtual int Id { get; set; }

public virtual string Nome { get; set; }

public virtual string Identificacao { get; set; }

public virtual IList<Pessoa> Gerentes { get; protected set; }

public Agencia()
{
Gerentes = new List<Pessoa>();
}

public virtual void AddGerente(Pessoa gerente)
{
Gerentes.Add(gerente);
}
public virtual void AddGerentes(params Pessoa[] gerentes)
{
Parallel.ForEach(gerentes, (pessoa) => Gerentes.Add(pessoa));
}
}

public class Pessoa: IEntity
{
public virtual int Id { get; set; }

public virtual string Nome { get; set; }

}

使用此约定(定义为 set AsSet )
public class AgenciaConvention : IAutoMappingOverride<Agencia>
{
public void Override(AutoMapping<Agencia> mapping)
{
mapping.HasManyToMany(a => a.Gerentes).Cascade.AllDeleteOrphan().AsSet().Not.Inverse();
}
}

当我运行此测试时:
[TestMethod]
[Description("Uma agência tem vários gerêntes")]
public void AgenciaTemVariosGerentes()
{
// Arrange
var fix = new Fixture();
var currentUser = GetLoggedUser();

// Create a List<Pessoa>

var gerentes = fix.Build<Pessoa>()
.With(p => p.Nome)
.With(p => p.CPF)
.With(p => p.CreateBy, currentUser)
.OmitAutoProperties()
.CreateMany<Pessoa>(10).ToList();

// Action

new PersistenceSpecification<Agencia>(Session)
.CheckProperty(p => p.Nome, fix.Create<string>().Truncate(80))
.CheckProperty(p => p.Identificacao, fix.Create<string>().Truncate(10))
.CheckReference(p => p.Regional,
fix.Build<Regional>()
.With(p => p.Nome)
.OmitAutoProperties()
.Create()
, new IDEqualityComparer())
.CheckList(p => p.Gerentes, gerentes, new IDEqualityComparer())
.CheckReference(p => p.CreateBy, currentUser, new IDEqualityComparer())
.VerifyTheMappings(); // Assert
}

如何测试此列表?

The collection should be AsSet, it necessary that the Parent and Children fields are PK, FK



完整错误:

测试名称:AgenciaTemVariosGerentes
测试全名:{OMMITED}.Integration.Test.AgenciaTest.AgenciaTemVariosGerentes
测试源:{OMMITED}.Integration.Test\AgenciaTest.cs:第 22 行
测试结果:失败
测试时长:0:00:02,4093555

结果信息:
测试方法 {OMMITED}.Integration.Test.AgenciaTest.AgenciaTemVariosGerentes 抛出异常:
NHibernate.PropertyAccessException: Invalid Cast(检查您的映射是否存在属性类型不匹配); CreditoImobiliarioBB.Model.Regional 的 setter ---> System.InvalidCastException:无法转换类型为 'NHibernate.Collection.Generic.PersistentGenericSet 1[CreditoImobiliarioBB.Model.Pessoa]' to type 'System.Collections.Generic.IList 的对象1 [CreditoImobiliarioBB.Model.Pessoa]'。
结果堆栈跟踪:
at (Object , Object[] , SetterCallback )
在 NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues(Object target, Object[] values)
在 NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values)
--- 内部异常堆栈跟踪结束 ---
在 NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values)
在 NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValues(Object entity, Object[] values)
在 NHibernate.Persister.Entity.AbstractEntityPersister.SetPropertyValues(Object obj, Object[] values, EntityMode entityMode)
在 NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(对象实体,EntityKey 键,IEntityPersister 持久化器, bool useIdentityColumn,对象任何东西,IEventSource 源, bool requiresImmediateIdAccess)
在 NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object entity, Object id, IEntityPersister persister, Boolean useIdentityColumn, Object any, IEventSource source, Boolean requiresImmediateIdAccess)
在 NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object any, IEventSource source, Boolean requiresImmediateIdAccess)
在 NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent 事件)
在 NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent 事件)
在 NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent 事件)
在 NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent 事件)
在 NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent 事件)
在 NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent 事件)
在 NHibernate.Impl.SessionImpl.Save(Object obj)
在 FluentNHibernate.Testing.PersistenceSpecification 1.TransactionalSave(Object propertyValue)
at FluentNHibernate.Testing.Values.ReferenceProperty
2.HasRegistered(PersistenceSpecification 1 specification)
at FluentNHibernate.Testing.PersistenceSpecification
1.RegisterCheckedProperty(Property 1 property, IEqualityComparer equalityComparer)
at FluentNHibernate.Testing.PersistenceSpecificationExtensions.CheckReference[T](PersistenceSpecification
1 spec, Expression`1 expression, Object propertyValue, IEqualityComparer propertyComparer)
在 CreditoImobiliarioBB.Repository.Integration.Test.AgenciaTest.AgenciaTemVariosGerentes() 在 {OMMITED}.Integration.Test\AgenciaTest.cs:line 27

谢谢。

最佳答案

集合不实现 IList<T> .

将您的属性定义为 ICollection<T>反而。

关于unit-testing - 无法将 'NHibernate.Collection.Generic.PersistentGenericSet` 1 转换为 System.Collections.Generic.IList`1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16742972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com