当我必须对一个调用其他类的多个公共方法的方法进行单元测试时,我感到非常困惑。这是示例
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SkillKindle.BLL;
using SkillKindle.BLL.ClassDetails;
using SkillKindle.BLL.SkClasses;
using SkillKindle.Domain.Models.SkClasses;
using SkillKindle.Infrastructure;
using SkillKindle.Web.Core.Infrastructure.ErrorHandling;
using SkillKindleWeb.Mappers;
using SkillKindleWeb.ViewModels.ClassDetails;
namespace SkillKindleWeb.Controllers
{
[CustomHandleError(ExceptionType = typeof (BusinessValidationException))]
public class ClassDetailsController : BaseController
{
private readonly ILogger _logger;
private readonly IMapperService _mapperService;
private readonly IAccessorFactory _accessorFactory;
private const int RegistrationId = 34;
private IClassDetailsAccessor ClassDetailsAccessor
{
get { return _accessorFactory.CreateClassDetailsAccessor(); }
}
private ISkClassAccessor SkClassAccessor
{
get { return _accessorFactory.CreateSkClassAccessor(); }
}
private IClassCreativeAccessor ClassCreativeAccessor
{
get { return _accessorFactory.CreateClassCreativeAccessor(); }
}
public ClassDetailsController(ILogger logger, IMapperService mapperService,
IAccessorFactory accessorFactory)
{
_logger = logger;
_mapperService = mapperService;
_accessorFactory = accessorFactory;
}
public ViewResult Index(int classCreativeId)
{
var classCreative = ClassCreativeAccessor.GetClassCreative(classCreativeId);
if (classCreative == null)
{
throw new HttpException(404, "The url is not valid");
}
var batches = ClassCreativeAccessor.GetFutureBatches(classCreativeId);
IList<ClassTicket> tickets = new List<ClassTicket>();
IList<Venue> venues = new List<Venue>();
if (batches.Count > 0)
{
tickets =
ClassCreativeAccessor.GetTickets(
batches.Select(batch => batch.ClassScheduleId).Distinct().ToArray());
venues = SkClassAccessor.GetVenues(batches.Select(batch => batch.VenueId).Distinct().ToArray());
}
var classDetailsViewModel = _mapperService.ClassCreativeToClassDetailsViewModel(classCreative);
var batchViewModels = _mapperService.BatchToClassDetailsBatchViewModel(batches).ToList();
var ticketViewModels = _mapperService.ClassTicketToClassDetailsTicketViewModel(tickets).ToList();
var venueViewModels = _mapperService.VenueToClassDetailsVenueViewModel(venues).ToList();
var indexViewModel = new IndexViewModel()
{
Batches = batchViewModels,
Tickets = ticketViewModels,
ClassDetails = classDetailsViewModel,
Venues = venueViewModels
};
return View(indexViewModel);
}
}
}
这里的Index方法依赖于mapperService,SkClassAccessor,ClassDetailsAccessor, ClassCreativeAccessor公共方法。我已经单独测试了那些公共方法。现在,在测试Index方法时,我需要检查indexViewModel的正确性。以下是我的几个选择。
选项1.模拟依赖类的公共方法以返回伪对象,并检查IndexViewModel是否具有那些伪对象。我不确定这是不是真正的考验。此外,它不会测试我是否将写入参数传递给这些模拟公共方法。
选项2.不要模拟依赖类的公共方法,而是伪造依赖类的依赖关系。例如假冒ClassCreativeAccessor.GetTickets可以操作的门票列表。这种方法将验证我将正确的参数传递给依赖的公共方法。但在这里我将再次测试公共方法
我不确定哪种方法是正确的。感谢你的帮助。
答案 0 :(得分:2)
我不确定这是否是真正的考验。
这是应该进行的单元测试。不要将它与集成测试混在一起。
此外,它不会测试我是否将写入参数传递给它们 模拟公共方法。
当您模拟依赖项(第一个选项)时,您始终可以验证是否使用适当的参数调用了方法。例如。与Moq:
mock.Verify(foo => foo.Execute("ping"));
将检查是否使用参数Execute
调用了依赖项foo
的方法"ping"
。同样,您可以使用适当的参数验证您的ClassCreativeAccessor
是否已被调用:
int classCreativeId = 42;
List<Batch> batches = new List<Batch>();
creativeAccessorMock.Setup(ca => ca.GetFutureBatches(classCreativeId))
.Returns(batches);
...