gpt4 book ai didi

c# - 单元测试重构

转载 作者:行者123 更新时间:2023-12-03 19:15:18 37 4
gpt4 key购买 nike

我在为业务逻辑层编写单元测试时遇到了一些问题,请为我指出正确的方向。任何意见,将不胜感激。

业务逻辑

public class TitleLogic
{
private readonly TitleDAL titleDAL = new TitleDAL();
private readonly List<TitleEntity> titleEntities;

public TitleLogic()
{
titleEntities = titleDAL.GetAllTitles().ToList();
}

public TitleEntity InsertTitle(TitleEntity titleEntity)
{
if (!titleEntity.IsValid)
{
throw new EntityException<TitleEntity>("Invalid Title.", titleEntity);
}

titleEntity.TitleName.TrimSize(TitleEntity.TitleName_Length);

var createdTitle = titleDAL.InsertTitle(titleEntity);

titleEntities.Add(createdTitle);

return createdTitle;
}

public TitleEntity FindTitle(string titleName)
{
return titleEntities.Find(p => p.TitleName == titleName);
}
}

数据层

public class TitleDAL
{
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

public TitleEntity InsertTitle(TitleEntity titleEntity)
{
XTime900Entities xTime900Entities = new XTime900Entities();

//Find the next CodeId to use
var titleCodeId = xTime900Entities.TITLEs.Max(p => p.TITLE_CODEID) + 1;

TITLE title = new TITLE
{
TITLE_CODEID = (short) titleCodeId,
TITLE_ACTIVE = Convert.ToInt16(titleEntity.TitleActive),
TITLE_NAME = titleEntity.TitleName
};

xTime900Entities.TITLEs.InsertOnSubmit(title);
xTime900Entities.SubmitChanges();
logger.Debug("Inserted New Title CodeId: {0}", titleCodeId);
xTime900Entities.Dispose();

return titleEntity.Clone((short)titleCodeId);
}

public ICollection<TitleEntity> GetAllTitles()
{
logger.Debug("Retrieving List all Titles from XTime900 database.");
List<TitleEntity> titleEntities = new List<TitleEntity>();

using (XTime900Entities XTEntities = new XTime900Entities())
{
var titlesInDB = from p in XTEntities.TITLEs
select p;

foreach (var titlesInDb in titlesInDB)
{
TitleEntity genderEntity = new TitleEntity(titlesInDb.TITLE_CODEID)
{
TitleActive = Convert.ToBoolean(titlesInDb.TITLE_ACTIVE),
TitleName = titlesInDb.TITLE_NAME
};

titleEntities.Add(genderEntity);
}
}

logger.Debug("Found {0} Titles.", titleEntities.Count);
return titleEntities;
}
}

实体

public class TitleEntity
{
public const int TitleName_Length = 30;

public short TitleCodeId { get; private set; }
public bool TitleActive { get; set; }
public string TitleName { get; set; }
public bool IsValid
{
get
{
return !String.IsNullOrEmpty(TitleName);
}
}

public TitleEntity()
{
this.TitleCodeId = -1;
}
public TitleEntity(short titleCodeId)
{
this.TitleCodeId = titleCodeId;
}

public TitleEntity Clone(short titleCodeId)
{
TitleEntity genderEntity = new TitleEntity(titleCodeId)
{
TitleActive = this.TitleActive,
TitleName = this.TitleName
};

return genderEntity;
}

public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(String.Format("TitleCodeId : {0}", TitleCodeId));
sb.AppendLine(String.Format("TitleActive : {0}", TitleActive));
sb.AppendLine(String.Format("TitleName : {0}", TitleName));
return sb.ToString();
}

public static bool operator ==(TitleEntity x, TitleEntity y)
{
return (x.Equals(y));
}

public static bool operator !=(TitleEntity x, TitleEntity y)
{
return !(x.Equals(y));
}

public bool Equals(TitleEntity other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return other.TitleCodeId == TitleCodeId && other.TitleActive.Equals(TitleActive) && Equals(other.TitleName, TitleName);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj.GetType() == typeof(TitleEntity) && Equals((TitleEntity)obj);
}

public override int GetHashCode()
{
unchecked
{
var result = TitleCodeId.GetHashCode();
result = (result * 397) ^ TitleActive.GetHashCode();
result = (result * 397) ^ (TitleName != null ? TitleName.GetHashCode() : 0);
return result;
}
}
}

最佳答案

您无法轻松测试您的业务逻辑,因为您已经在 TitleLogic 中创建了 DAL 组件。类。

我要做的第一件事就是制作 TitleDAL实现 ITitleDAL接口(interface)和制作 TitleLogic类采用 ITitleDAL 的实例界面。

然后当您测试InsertTitle时您可以通过以下方法进行测试:

  • 检查当 TitleEntity 无效时给出 EntityException被抛出
  • 当实体有效时 ITitleDAL.InsertTitle被称为。
  • 正确插入标题后,可以使用FindTitle找到它。方法

在您的测试中,您需要创建一个模拟 ITitleDAL实现(或使用 mocking library 为您创建一个),以便您的测试返回已知的预期数据,而不依赖于实际的 DAL。

您可能还想考虑测试:

  • 如果InsertTitle会发生什么方法ITitleDAL失败?

关于c# - 单元测试重构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5537845/

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