yams
yams

Reputation: 952

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.
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...

Upvotes: 1

Views: 767

Answers (1)

yams
yams

Reputation: 952

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);

    }

Upvotes: 1

Related Questions