Trigger API - C# Code Unit testing

Rock Hitman 46 Reputation points
2021-11-02T15:58:23.83+00:00

Hi, I need help to debug the Controllers class code where I put the debug points.
Need to create a class library to write Unit test code for me to set this as startup project and trigger.

Below is my Controllers class code....can anyone pls help me with writing Unit test code for me to trigger the Controllers class ?

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using Microsoft.Web.Http;
using GeicoApiGate.Domain.Models;
using GeicoApiGate.Helpers;
using GeicoApiGate.Repository;
using GeicoApiGate.Repository.Interfaces;
using System.IO;

namespace GeicoApiGate.Controllers
{

 [RouteAttribute("txtFileUpload")]
        [ResponseType(typeof(ApiResponse))]
        [HttpPost]
        public async Task<HttpResponseMessage> MultiPartMimeFileUpload(HttpRequestMessage request)
        {
            HttpResponseMessage responseMessage;
            var responseObject = new ApiResponse();

           var Authentication = true; ;

            if (Authentication)
            {
                // Check if the request contains multipart/form-data if it doesn't log information
                if (!Request.Content.IsMimeMultipartContent())
                {

                    if (request.Content.Headers.ContentType != null && !string.IsNullOrEmpty(request.Content.Headers.ContentType.ToString()))
                    {
                        responseObject.message = string.Format("Unsupported Media Type of {0} passed in", request.Content.Headers.ContentType);
                    }
                    else
                    {
                        responseObject.message = "Unsupported Media Type passed in";
                    }
                }
            }
        }

}
Developer technologies ASP.NET Other
Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2021-11-03T07:22:27.237+00:00

    Hi @Rock Hitman ,
    Regarding unit testing, you can want to make arrangements for the controller, and then get the results you need, and then you can test,You can refer to the following code:
    https://learn.microsoft.com/en-us/aspnet/web-api/overview/testing-and-debugging/unit-testing-with-aspnet-web-api

    [TestMethod]  
    public void GetReturnsProduct()  
    {  
        // Arrange  
        var controller = new ProductsController(repository);  
        controller.Request = new HttpRequestMessage();  
        controller.Configuration = new HttpConfiguration();  
        // Act  
        var response = controller.Get(10);  
        // Assert  
        Product product;  
        Assert.IsTrue(response.TryGetContentValue<Product>(out product));  
        Assert.AreEqual(10, product.Id);  
    }  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. AgaveJoe 30,126 Reputation points
    2021-11-03T11:19:26.677+00:00

    Your code sample is not unit testable because it attempts to test the HTTP protocol which is already well tested and known to work properly. However, you can test the MultiPartMimeFileUpload method using an integration test. The integration test will use an HTTP client to invoke the MultiPartMimeFileUpload action. Examples of HTTP clients are PostMan, the C# HttpClient, a browser, etc.

    If the intent is to unit test file upload then replace the HttpRequestMessage parameter with IFormFile. Mock the IFromFile to include whatever data is under test.

    How to mock an IFormFile for a unit/integration test in ASP.NET Core?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.