0

I am attempting to create a unit test that checks for a file (e.g. http://images/icon.png) in an asp.net core project. I know how to do it for a specific controller for example.

    [TestMethod]
    public void WebApp_GetFoo()
    {
        var controller = new HomeController();
        var result = (OkObjectResult)controller.GetFoo();
        Assert.AreEqual(200, result.StatusCode);

        dynamic resultModel = DynamicExtensions.ToDynamic(result.Value);

        Assert.AreEqual("The Quick Brown Fox Jumped Over The Lazy Dog", resultModel.Text);
    }

But how does one do it for a simple GET request.

howardlo
  • 1,401
  • 1
  • 15
  • 20
  • You just call the method. You don't need to create `GET` request. – FCin Aug 27 '17 at 15:39
  • What exactly is it you want to test? Do you have an example of the code to be tested? – Kirk Larkin Aug 27 '17 at 15:40
  • For example, I have some image files that I want to make sure that they're there like http:///images/icon.png. I guess I can just use FileExists but I would like to run through the stack to make sure routes are correct, etc. Thanks. – howardlo Aug 27 '17 at 15:58
  • you just want to check the file exists or also the contents? – derloopkat Aug 27 '17 at 16:00
  • @FCin, it might be quite obvious but I'm drawing a blank. what method do I call if it's the default file handler? Thanks. – howardlo Aug 27 '17 at 16:00
  • @derloopkat, would like to pull it through the stack and then check the content. – howardlo Aug 27 '17 at 16:06
  • 3
    You are most likely not looking for a unit test, but an integration test: https://learn.microsoft.com/en-us/aspnet/core/testing/integration-testing – Tseng Aug 27 '17 at 16:17
  • you can do WebClient client = new WebClient(); client.DownloadFile(targetUrl, fileDestinationPathname); and use a try/catch block for catching http errors. – derloopkat Aug 27 '17 at 16:34
  • @Tseng, thanks! just what I was looking for. I did the example with the "empty web" template returning a simple "Hello World" and it tests fine. But having issues with the test in an app created with "web application (mvc)" template because it could not find "/Views/Home/Index.cshtml". Looks like a mapping issue and looking into it... Tseng, if you have any ideas, please let me know. Thanks! – howardlo Aug 27 '17 at 17:45
  • Found how to use the UseContentRoot in WebHostBuilder so it's working with Asp.Mvc apps. The examples are on my GitHub: https://github.com/howardlo/AspNetCore-MVC-IntegrationTest – howardlo Aug 27 '17 at 20:45
  • @hlo, your action method is returning type *OkObjectResult* having StatusCode as a property. But the actual http response already has a StatusCode so that's redundant. Instead of having a property into this object, you should be setting StatusCode of the http response. See for instance this https://stackoverflow.com/a/12112595/2516718 This is returning Json and also setting StatusCode the correct way. – derloopkat Aug 28 '17 at 23:04
  • @derloopkat, good point. Thanks! – howardlo Aug 29 '17 at 18:58

0 Answers0