3

当我必须对调用其他类的多个公共方法的方法进行单元测试时,我会感到非常困惑。这是示例

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 将操作的票证列表。这种方法将验证我是否将正确的参数传递给依赖的公共方法。但在这里我将再次测试公共方法

我不确定哪种方法是正确的。感谢您的帮助。

4

1 回答 1

2

我不确定这是否是一个真正的测试。

这是一个应有的单元测试。不要将其与集成测试混为一谈。

它也没有测试我是否将写入参数传递给这些模拟公共方法。

当您模拟依赖项(第一个选项)时,您始终可以验证是否使用适当的参数调用了方法。例如起订量:

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);
...
于 2013-07-01T14:08:59.327 回答