如何使用 Moq 来模拟一个简单的界面?
好的,我有一个如下所示的业务逻辑类:
注意:对于上下文,供应商简介是描述 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 控制器混淆),但我不知道该怎么做它。使用我自己的一些代码的示例将真正帮助我入门。
提前致谢!
Okay, I have a business logic class like this:
Note: For context, Vendor Briefs are simple entities that describe a "download" for a PDF document.
/// <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();
}
}
I am taking my first steps into unit testing and I'm learning about Moq. I don't want a whole unit test class written for this (unless you feel like it of course :P) but a simple sample will do. I'm assuming I need to "mock" IVendorBriefRepository
so that I can pass it into the constructor when building my controller (not to be confused with mvc controllers), but I'm not sure how to do it. A sample using some of my own code will really help me get started.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
例如,类似的事情可以测试DeleteVendorBrief。
Something like this would test DeleteVendorBrief, for example.
如果您不需要为 Mocked 对象设置任何特殊行为,而只想将伪造的对象传递到简单的单元测试中,则还可以使用较短的
Mock.Of()
句法。这样做的好处是您不需要记住传递
.Object
。以 Brook 的代码作为参考:
特别是如果您的代码使用依赖注入并传入很多依赖项,这种语法会使代码更清晰易读。
If you don't need to setup any special behavior for your Mocked object and just want a faked object to pass into a simple unit test, you can also use the shorter
Mock.Of<T>()
syntax.The benefit here is that you don't need to remember to pass in the
.Object
.Taking Brook's code as a reference:
Especially if your code uses Dependency Injection and pass in a lot of dependencies, this syntax makes the code cleaner to read.