使用xUnit和Moq对ASP.Net Core Web API进行单元测试

问题描述 投票:1回答:1

我正在尝试使用xUnit和Moq测试我的Web API。我使用构造函数实例化了所有必需的模拟存储库实例。我还在构造函数中实例化了控制器。控制器实例成功调用了action方法。但是没有在我的存储库中执行函数。

public class SubjectControllerTest
{
    public Mock<IHostingEnvironment> HostingEnvironment { get; set; }
    private readonly Mock<ILogger<SubjectController>> _logger;
    private readonly Mock<ISubjectManager<Subject>> _subjectManager;
    private readonly Mock<IChapterManager<Chapter>> _chapterManager;
    private readonly Mock<ITopicManager<Topic>> _topicManager;
    private readonly Mock<ILearningPointManager<LearningPoint>> _learningPointManager;
    private SubjectController controller;

    public SubjectControllerTest()
    {
        _subjectManager = new Mock<ISubjectManager<Subject>>();
        _chapterManager = new Mock<IChapterManager<Chapter>>();
        _topicManager = new Mock<ITopicManager<Topic>>();
        _learningPointManager = new Mock<ILearningPointManager<LearningPoint>>();
        _logger = new Mock<ILogger<SubjectController>>();
        HostingEnvironment = new Mock<IHostingEnvironment>();
        controller = new SubjectController(_subjectManager.Object, _chapterManager.Object,
                                             _topicManager.Object, _learningPointManager.Object, HostingEnvironment.Object, _logger.Object);
    }

    [Fact]
    public void Test_GetSubjectList()
    {   
        var subject1 = new Subject
        {
            Subject_ID = 1,
            Subject_Name = "Physics",
            Subject_Photo_Url = null,
            Validity_Start_Date = Convert.ToDateTime("2/2/2018 12:00:00 AM"),
            Validity_End_Date = Convert.ToDateTime("12/25/2018 12:00:00 AM")
        };
        var subject2 = new Subject
        {
            Subject_ID = 2,
            Subject_Name = "Business",
            Subject_Photo_Url = null,
            Validity_Start_Date = Convert.ToDateTime("3/10/2018 12:00:00 AM"),
            Validity_End_Date = Convert.ToDateTime("3/20/2018 12:00:00 AM")
        };

        var subject3 = new Subject
        {
            Subject_ID = 3,
            Subject_Name = "Math",
            Subject_Photo_Url = "",
            Validity_Start_Date = Convert.ToDateTime("7/8/2018 8:33:23 AM"),
            Validity_End_Date = Convert.ToDateTime("5/8/2019 8:33:23 AM")
        };

        List<Subject> subjectList = new List<Subject>();
        subjectList.Add(subject1);
        subjectList.Add(subject2);
        subjectList.Add(subject3);

        int totalPages = 0;
        _subjectManager.Setup(x => x.GetList(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>(), ref totalPages))
            .Returns(subjectList);

        // Act
        var result = controller.GetSubjectList(1, 5, string.Empty, string.Empty);

        // Assert
        var okResult = Assert.IsType<JsonResult>(result);
        var viewModel = Assert.IsType<Paging>(okResult.Value);
        var list = viewModel.Data as List<Subject>;

        Assert.Equal(3, list.Count);
    }
}

我在这里打电话给GetSubjectList动作法。已成功调用该方法,但未执行存储库方法GetList()。我的API如下。

可能是什么问题?

[HttpGet]
[Route("api/subjects")]
public IActionResult GetSubjectList(int page, int pageSize, string search, string sort)
{
    try
    {
        if (search == null)
            search = "";

        if (sort == null)
            sort = "";

        int totalPages = 0;

        List<Subject> list = _subjectManager.GetList(page, pageSize, sort, search, ref totalPages).ToList();

        Paging result = new Paging { Data = list, Total = totalPages };

        return Json(result);
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message, ex);
    }

}
unit-testing asp.net-core moq asp.net-core-webapi xunit
1个回答
0
投票

ref totalPages的设置导致参数不匹配,因为在测试方法中使用的实例不相同。只有在调用的ref参数与设置期间使用的实例相同时,它才会匹配。

如果使用Moq 4.8或更高版本,请使用It.Ref<T>.IsAny

_subjectManager
    .Setup(_ => _.GetList(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>(), ref It.Ref<int>.Any))
    .Returns(subjectList);

这样,当调用模拟成员时,它将匹配请求的参数并按预期运行。

© www.soinside.com 2019 - 2024. All rights reserved.