さて、私はこのようなビジネスロジッククラスを持っています:
注:コンテキストでは、ベンダーブリーフはPDFドキュメントの「ダウンロード」を説明する単純なエンティティです。
/// <summary>
/// Houses business level functions for dealing with vendor briefs.
/// </summary>
public class VendorBriefController : IVendorBriefController
{
/// <summary>
/// Vendor brief controller requires an instance of IVendorBriefRepository.
/// </summary>
IVendorBriefRepository _vendorBriefRepository;
/// <summary>
/// Initializes an instance of VendorBriefController.
/// </summary>
public VendorBriefController(IVendorBriefRepository vendorBriefRepository)
{
_vendorBriefRepository = vendorBriefRepository;
}
/// <summary>
/// Get a list of string filters for vendor briefs.
/// </summary>
/// <returns>A list of string filters.</returns>
public dynamic GetFilters()
{
List<string> filters = new List<string>
{
"All",
"Active",
"Inactive"
};
return filters;
}
/// <summary>
/// Retrieve vendor brief entity from the repository by its unique ID.
/// </summary>
/// <param name="Id">The unique ID of the vendor brief.</param>
/// <returns>A vendor brief entity.</returns>
public VendorBrief GetVendorBriefForEditing(int Id)
{
var vendorBrief = _vendorBriefRepository.GetVendorBrief(Id);
return vendorBrief;
}
/// <summary>
/// Get a dynamic list of vendor briefs from the repository based on the supplied filter.
/// </summary>
/// <param name="filter">The filter to be used when retrieving vendor briefs.</param>
/// <returns>A dynamic sorted & filtered list of vendor briefs to be displayed in a grid view.</returns>
public dynamic GetVendorBriefList(string filter)
{
IEnumerable<VendorBrief> results = _vendorBriefRepository.GetVendorBriefs();
switch (filter)
{
default:
results = _vendorBriefRepository.GetVendorBriefs();
break;
case "Active":
results = _vendorBriefRepository.GetVendorBriefs(true);
break;
case "Inactive":
results = _vendorBriefRepository.GetVendorBriefs(false);
break;
}
return from x in results
orderby x.DisplayOrder
select new
{
ID = x.VendorBriefID,
Title = x.Title,
Active = x.IsActive,
DisplayOrder = x.DisplayOrder
};
}
/// <summary>
/// Save changes to the underlying repository in order to persist changes made to self-tracking vendor brief entities.
/// </summary>
/// <param name="vendorBrief"></param>
public void EditVendorBrief(VendorBrief vendorBrief)
{
_vendorBriefRepository.SaveChanges();
}
/// <summary>
/// Remove a vendor brief from the underlying repository.
/// </summary>
/// <param name="vendorBrief">The vendor brief to be removed.</param>
public void DeleteVendorBrief(VendorBrief vendorBrief)
{
_vendorBriefRepository.DeleteVendorBrief(vendorBrief);
_vendorBriefRepository.SaveChanges();
}
/// <summary>
/// Add a vendor brief to the underlying repository.
/// </summary>
/// <param name="vendorBrief">The vendor brief to be added.</param>
public void AddVendorBrief(VendorBrief vendorBrief)
{
_vendorBriefRepository.AddVendorBrief(vendorBrief);
_vendorBriefRepository.SaveChanges();
}
}
私はユニットテストに最初のステップを踏んでおり、Moqについて学習しています。 (もちろん:Pのように感じない限り)ユニットテストクラス全体をこのために記述したくありませんが、簡単なサンプルで十分です。コントローラーを構築するときにコンストラクターに渡すことができるようにIVendorBriefRepository
を「モック」する必要があると想定しています(mvcコントローラーと混同しないでください)。しかし、その方法はわかりません。私自身のコードの一部を使用したサンプルは、実際に始めるのに役立ちます。
前もって感謝します!
たとえば、このようなものはDeleteVendorBriefをテストします。
Mock<IVendorBriefRepository> mock = new Mock<IVendorBriefRepository>();
VendorBriefController controller = new VendorBriefController(mock.Object);
VendorBrief brief = new VendorBrief();
controller.DeleteVendorBrief(brief);
mock.Verify(f=>f.DeleteVendorBrief(brief));
mock.Verify(f=>f.SaveChanges());