简体   繁体   English

如何使用 XUnit 对 Web API controller 进行单元测试

[英]How to unit test a Web API controller using XUnit

I am trying to unit test a method within my controller in my Web API using XUnit.我正在尝试使用 XUnit 在我的 Web API 中对我的 controller 中的方法进行单元测试。 The role of the method is to get a single title, by ISBN, from the database.该方法的作用是从数据库中按 ISBN 获取单个标题。 The issue I came across during unit testing is that I am unsure how to insert the dummy data that I must perform the test on, as well as how the Assert function works.我在单元测试期间遇到的问题是我不确定如何插入我必须执行测试的虚拟数据,以及 Assert function 如何工作。

TitleController.cs标题控制器.cs

[ApiController]
[Route("titlecontroller")]
public class TitleController : Controller
{
    private IGtlTitleRepository _gtlTitleRepository;

    public TitleController(IGtlTitleRepository gtlTitleRepository)
    {
        _gtlTitleRepository = gtlTitleRepository;
    }

    [Route("getTitle/{ISBN}")]
    [HttpGet()]
    public GtlTitle GetTitle(string ISBN)    
    {
        return _gtlTitleRepository.GetTitle(ISBN);
    }
}

IGtlTitleRepository.cs IGtlTitleRepository.cs

    public interface IGtlTitleRepository
{
    GtlTitle GetTitle(string ISBN);
}

MockGtlTitleRepository.cs MockGtlTitleRepository.cs

    public class MockGtlTitleRepository : IGtlTitleRepository
{
    private readonly string _connection;
    public MockGtlTitleRepository(IOptions<ConnectionStringList> connectionStrings)
    {
        _connection = connectionStrings.Value.GTLDatabase;
    }


    private List<GtlTitle> _titleList;

    public GtlTitle GetTitle(string ISBN)
    {
        using (var connection = new SqlConnection(_connection))
        {
            connection.Open();
            return connection.QuerySingle<GtlTitle>("GetTitleByISBN", new { ISBN }, commandType: CommandType.StoredProcedure);
        }
    }

}

Right, as for my test code, I was able to write the following code, but as I said above, I can't figure out a proper way to test the method.对,至于我的测试代码,我能够编写以下代码,但正如我上面所说,我无法找到测试该方法的正确方法。

 public class UnitTest1
{
    [Fact]
    public void Test1()
    {

        var repositoryMock = new Mock<IGtlTitleRepository>();
        var title = new GtlTitle();
        repositoryMock.Setup(r => r.GetTitle("978-0-10074-5")).Returns(title);
        var controller = new TitleController(repositoryMock.Object);

        var result = controller.GetTitle("978-0-10074-5");
       // assert??
        repositoryMock.VerifyAll();
    }
}

What should be done within this unit test in order to properly test the method?为了正确测试方法,在这个单元测试中应该做什么?

EDIT:编辑:

GtlTitle.cs GtlTitle.cs

public class GtlTitle
{
    public string ISBN { get; set; }
    public string VolumeName { get; set; }
    public string TitleDescription { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string PublisherName { get; set; }

}

Before going to testing, there are a few things I recommend updating in your code:在进行测试之前,我建议在您的代码中更新一些内容:

  • Make your repository methods and controller actions async (thus web server can process requests while waiting for database roundtrips for previous calls)使您的存储库方法和 controller 操作异步(因此 web 服务器可以在等待先前调用的数据库往返时处理请求)
  • Use ActionResult as an action return type.使用ActionResult作为操作返回类型。 This way you can send different http status codes to the client.这样您就可以向客户端发送不同的 http 状态码。
  • Return 404 NotFound status code when title not found instead of returning successful result with null as payload.未找到标题时返回 404 NotFound 状态码,而不是返回成功结果,其中null作为有效负载。
  • Consider using a RESTful approach for API endpoints.考虑对 API 端点使用 RESTful 方法。 Eg base uri for titles resource should be something like api/titles例如,titles 资源的基本 uri 应该类似于api/titles
  • Don't specify getTitle for getting title endpoint, because you know HTTP verb which endpoint is mapped to (GET) and base resource url (api/titles).不要指定getTitle来获取标题端点,因为您知道 HTTP 动词哪个端点映射到 (GET) 和基本资源 url (api/titles)。

With these notes applied:应用这些注释:

[ApiController]
[Route("api/titles")]
public class TitleController : Controller
{
    private IGtlTitleRepository _gtlTitleRepository;

    public TitleController(IGtlTitleRepository gtlTitleRepository)
    {
        _gtlTitleRepository = gtlTitleRepository;
    }

    [HttpGet("{ISBN}")] // GET api/titles/{ISBN}
    public async Task<ActionResult<GtlTitle>> GetTitle(string ISBN)    
    {
        var title = await _gtlTitleRepository.GetTitle(ISBN);
        if (title == null)
            return NotFound();

        return title;
    }
}

Testing successful title retrieving:测试成功的标题检索:

[Fact]
public async Task Should_Return_Title_When_Title_Found()
{
    var repositoryMock = new Mock<IGtlTitleRepository>();
    var title = new GtlTitle();
    repositoryMock.Setup(r => r.Get("978-0-10074-5")).Returns(Task.FromResult(title));

    var controller = new TitleController(repositoryMock.Object);

    var result = await controller.GetTitle("978-0-10074-5");
    Assert.Equal(title, result.Value);
}

When title not found:找不到标题时:

[Fact]
public async Task Should_Return_404_When_Title_Not_Found()
{
    var repositoryMock = new Mock<IGtlTitleRepository>();
    repositoryMock.Setup(r => r.Get("978-0-10074-5")).Returns(Task.FromResult<GtlTitle>(null));

    var controller = new TitleController(repositoryMock.Object);

    var result = await controller.GetTitle("978-0-10074-5");
    Assert.IsType<NotFoundResult>(result.Result);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM