How do I Test Controller HTTP Methods with Nunit test?

Sarwar Akbar 31 Reputation points
2022-07-27T04:40:37.587+00:00

namespace CoreWebApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class DataPrizeController : ControllerBase
{
private readonly PrizeService service;
private readonly IRepository<Prize> _Prize;

    public DataPrizeController(PrizeService service, IRepository<Prize> _Prize)  
    {  
        this.service = service;  
        this._Prize = _Prize;  
    }  


    //Get All Prizes  
    [HttpGet("GetAll")]  
    public Object GetAllPrizes()  
    {  
        var data = service.GetAllPrizes();  
        return data;  
    }  

    //Add Prize    
    [HttpPost("AddPrize")]  
    public Object AddPrize([FromBody] Prize prize1)  
    {  
        try  
        {  
            service.Add(prize1);  
            return true;  
        }  
        catch (Exception)  
        {  

            return false;  
        }  
    }  

Above is my code. How do I write test methods as per above code? I have already researched regarding the same but not did not get relevant material. Please help.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
694 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,337 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,190 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Jack J Jun 24,276 Reputation points Microsoft Vendor
    2022-07-27T07:08:15.19+00:00

    @Sarwar Akbar , Welcome to Microsoft Q&A, Since your problem is related to nunit test, it is a 3rd party product. I recommend that you could ask the question in Github.

    Thanks for your understanding

    0 comments No comments

  2. Sarwar Akbar 31 Reputation points
    2022-07-27T07:20:06.49+00:00

    I already searched all things regarding this.

    You can provide solution in Microsoft Unit Test.

    Can You Please write code for same?

    0 comments No comments

  3. satya karki 986 Reputation points MVP
    2022-07-27T08:05:27.383+00:00

    Hi, below one is using xunit. however, I think you can check it and get an idea. You can create a test project and write test methods for your controller.
    https://www.c-sharpcorner.com/article/crud-operations-unit-testing-in-asp-net-core-web-api-with-xunit/

    0 comments No comments

  4. AgaveJoe 26,181 Reputation points
    2022-07-27T13:46:34.837+00:00

    Above is my code. How do I write test methods as per above code?

    The official documentation covers unit testing asp.net Core controllers.

    Unit test controller logic in ASP.NET Core

    You might consider using HttpRepl to test Web API. This is an integration test because you're actually making HTTP requests. The advantage is you are also testing the actual service implementation rather than a mock.

    Test web APIs with the HttpRepl

    I have already researched regarding the same but not did not get relevant material.

    You have to understand, the community has no idea what you have researched or tried. You must write a mock implementation for the injected services. Did you do this? Can you show us what you've tried?

    0 comments No comments