Please let me know How do I Mock my "HttpRequestMessage" object so that when my code flows traverse from nUnitTest it should not having NULL value for "this,Request,CreateResponse()".
My Controller class:
public class EodController : ApiControllerBase
{
private readonly IEodManager eodManager;
public EodController(IEodManager, eodManager)
{
this.eodManager = eodManager;
}
/// Execution date
/// Returns that contains a stream of JSON data.
[HttpGet]
[Route("")]
public async Task GetCheckAsync([Fromuri] DateTime executionDate)
{
try
{
int Status = await this.eodManager.GetStatus(executionDate);
will be always NULL value
var response = this.Request.CreateResponse(); // It failed Here "this.Request’ will be always NULL value
response.StatusCode = (Status == 1) ? HttpStatusCode.OK : (HttpStatusCode)207;
response. Content =
new PushStreamContent(
(stream, httpContext, transportContext) => this.eodManager.WriteToStream(stream, criteria, Status),
I have Tried following things from test Class, it did not work .
public async Task ShouldReturnOKStatus()
{
// Arrange
// Set up the mock
var EodManager, = new Mock();
DateTime executionDate = new Datelime(2023, 6, 27);
var data = "this will be JSON”;
var httpReguestMessage = new HttpRequestMessage();
httpRequestMessage Content= new StringContent(data, Encoding.UTF8, “application/json");
var command = new EodController(EodManager.Object); // Act
var response = await command.GetCheckAsync(executionDate);
Assent.AreEqual("System.Web.Http.Results.OkResult”, response.ToString());
}
Prasad RaveendranPosted Jun 28, 2023, 9:18 PM
I would request you to explore AutoFixture
https://www.nuget.org/packages/autofixture/
this would help you to set mock data
Kumar AUPosted Jun 28, 2023, 1:45 PM
Hi Deepak, Can you please also let me know how do I mock the below line - which is also avaialble in same controler
int Status = await this.eodManager.GetStatus(executionDate);
Please sugest me - I tried below code , but its errored in - Task , basically GetStatus is method returns INT value - 1 or 0
Error message -Task is a Type which is not valid in given context
eodManager.Setup(x => x.GetStatus(executionDate)).Returns(Task );
also I tried eodManager.Setup(x => x.GetStatus(executionDate)).ReturnsAsync(1);
But I still don't see 1 value is being passed to my controller.
Basically below is the code for GetStatus method which I need to mock
public async Task GetStatus(string executionDate)
{
int jobCompleted = await this.eodManager.GetStatus(executionDate);
return jobCompleted;
}
Kumar AUPosted Jun 28, 2023, 12:32 PM
Thank you for your time and suggestion Rajkiran Swain, I appreciate your response
Kumar AUPosted Jun 28, 2023, 12:27 PM
Thank you so much Deepak, It worked !!
Now I understood I was missing this assignment - controller.Request = httpRequestMessage;
Deepak RawatPosted Jun 28, 2023, 5:31 AM
HttpRequestMessageobject and avoid having a null value forthis.Requestin your unit test, you can use a mocking framework like Moq. Here's an example of how you can modify your unit test to mock theHttpRequestMessage: