here is the code for my test
[Fact]
public void ActivityController_GetStores_ReturnBadRequest()
{
var stores = new QueryStringParams();
var result = controllerWithNullServices.GetStores(stores);
var expected = result;
result.Should().BeOfType<BadRequestObjectResult>().Which.Should().BeEquivalentTo(expected);
}
GetStores(stores) function returns a BadRequestObjectResult which I need to expect in my code of test. As for now I’ve written expected = result which will setup the expected value to exactly match the result.
here is the code for my controller as well
public IActionResult GetStores([FromBody] Helper.QueryStringParams queryParam)
{
_customLoggerService.LogInformation($"Getting Activity Log with params :: {queryParam} ");
try
{
Helper.PagedResult<List<ActivityLogDto>> result = _activityLogService.GetActivityLog(queryParam);
return Ok(result);
}
catch (Exception ex)
{
string logMessage = $"Error getting Activity Log." +
$" n Message :: {ex.Message} n Exception :: {ex}";
_customLoggerService.LogError(logMessage);
return BadRequest(new
{
Errors = ex,
ex.Message
});
}
}
so here bad requests is being returned only when some sort of exception occurs in the code and further more BadRequest() is also over loaded as below
public override BadRequestObjectResult BadRequest(object error)
{
if (error is not string)
{
Dictionary<string, object> errorDictionary = error.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
.ToDictionary(prop => prop.Name, prop => prop.GetValue(error, null));
return base.BadRequest(new SharedResponse.Response()
{
Status = SharedResponse.Response.RequestStatus.Error,
Message = errorDictionary["Message"].ToString(),
Errors = errorDictionary["Errors"]
});
}
return base.BadRequest(new SharedResponse.Response()
{
Status = SharedResponse.Response.RequestStatus.Error,
Message = error.ToString(),
Errors = null
});
}
how can I manually create expected value for the test case as described above
I want to write the expected value for this code manually not just the type but complete expected value.
[Fact]
public void ActivityController_GetStores_ReturnBadRequest()
{
var stores = new QueryStringParams();
var result = controllerWithNullServices.GetStores(stores);
var expected = result;
result.Should().BeOfType<BadRequestObjectResult>().Which.Should().BeEquivalentTo(expected);
Ahad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.