question

RockHitman-2461 avatar image
0 Votes"
RockHitman-2461 asked AgaveJoe answered

Trigger API - C# Code Unit testing

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";
                     }
                 }
             }
         }
            
 }
dotnet-csharpdotnet-aspnet-mvc
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Consider mocking as per the following docs. Think of what is discussed as a starting point no matter if this is ASP.NET Core or not.


0 Votes 0 ·

there is so much c# code in it.

Is there a simple Unit test class I can create just to trigger the flow and hit the debug point on Controller class ?

0 Votes 0 ·
LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered LanHuang-MSFT edited

Hi @RockHitman-2461,
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://docs.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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered

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?


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.