@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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
@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
I already searched all things regarding this.
You can provide solution in Microsoft Unit Test.
Can You Please write code for same?
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/
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?