简体   繁体   English

使用 Xunit 测试 asp.net core 2.0 项目如何测试 httpget

[英]Testing asp.net core 2.0 project with Xunit how to test httpget

I've got a asp.net core 2.0 project I'm building in Visual Studio 2017. I'm learning the ropes with testing a asp.net core project but I'm wondering how to test the HHTPGET methods within my controller.我有一个在 Visual Studio 2017 中构建的 asp.net core 2.0 项目。我正在学习测试 asp.net core 项目的技巧,但我想知道如何在我的控制器中测试 HHTPGET 方法。
Below is my initial controller.下面是我的初始控制器。

public class ValuesController : Controller
{
    private Config MyConfig { get; }

    private Solr Solr { get; }

    private Voyager Voyager { get; }

    private Messages Messages { get; }

    public ValuesController(Config config, Solr solr, Voyager voyager, Messages messages)
    {
        MyConfig = config;
        Solr = solr;
        Voyager = voyager;
        Messages = messages;
    }

    // GET api/values
    [HttpGet]
    [Route("/api/[Controller]")]
    public IEnumerable<string> Get()
    {
        string version = ".NET Framework: " + Environment.Version.ToString();
        string vers = "ASP.NET Core Framework: " + typeof(Controller).Assembly.GetName().Version.ToString();
        return new string[] { version, vers };
    }

    [HttpGet]
    [Route("/api/[Controller]/config")]
    public JsonResult GetConfig()
    {
        return new JsonResult(MyConfig);
    }

    [HttpGet]
    [Route("/api/[Controller]/solr")]
    public JsonResult GetSolr()
    {
        return new JsonResult(Solr);
    }

    [HttpGet]
    [Route("/api/[Controller]/voyager")]
    public JsonResult GetVoyager()
    {
        return new JsonResult(Voyager);
    }

    [HttpGet]
    [Route("/api/[Controller]/messages")]
    public JsonResult GetMessages()
    {
        return new JsonResult(Messages);
    }

Just wondering if someone could give an example of how to test these routes or HTTPGets...只是想知道是否有人可以举例说明如何测试这些路由或 HTTPGets ......

So I found that the first step is to setup a mock connection that is somewhat generic so I created a method to mock the connection to a server via the method below.所以我发现第一步是设置一个有点通用的模拟连接,所以我创建了一个方法来通过下面的方法模拟到服务器的连接。

    private CollectionsController SetupController()
    {
        if (controller != null)
        {
            controller = null;
        }
        if (controllerContext != null)
        {
            controllerContext = null;
        }
        controller = new CollectionsController();
        controller.ControllerContext = new ControllerContext();
        controllerContext = controller.ControllerContext;
        controllerContext.HttpContext = new DefaultHttpContext();
        //The header below is generic don't really care what the device id is
        controllerContext.HttpContext.Request.Headers["device-id"] = "20317";

    }

Then as an example I've set up a test below that just checks the status code and asserts the object returning from the controller is not null.然后作为一个例子,我在下面设置了一个测试,它只检查状态代码并断言从控制器返回的对象不为空。

    [Fact]
    public void TestPostCollectionName()
    {
        valData = new ValData();
        valData.value = "NewObject";
        controller = SetupController();
        var results = controller.PostCollectionName(valData, testCollecName);
        Assert.NotNull(results);
        Assert.True(controllerContext.HttpContext.Response.StatusCode == 200);

    }

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

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