好的,我有一个像这样的业务逻辑类:
注意:就上下文而言,供应商简介是描述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。我不希望为此编写一个完整的单元测试类(当然,除非您喜欢它),但是一个简单的示例就可以了。我假设我需要"模拟"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());
如果您不需要为您的mock对象设置任何特殊行为,而只是想要一个伪造的对象通过一个简单的单元测试,您也可以使用较短的Mock.Of<T>()
语法。
这里的好处是您不需要记住传入.Object
。
参考Brook的代码:
IVendorBriefRepository mock = Mock.Of<IVendorBriefRepository>();
VendorBriefController controller = new VendorBriefController(mock);
//...
特别是当你的代码使用依赖注入并传入大量依赖项时,这种语法使代码读起来更清晰。