riki
riki

Reputation: 2403

How to test for NotFound http status in asp.net web api

I was trying to write a test case to check whether my action method is returning 404 status code.

So here my Web Api Action Method

[Route("{username}/{startDate}/{endDate}")]
[HttpGet]
public IHttpActionResult BulkTrackEventsByDateRange(string username, string startDate, string endDate)
{
    BulkEventTrackingResultModel bulkTrackingEventResult = null;
    try
    {
        bulkTrackingEventResult = _bulkTrackingByDateRange.GetBulkTrackingEvents(username, startDate, endDate);
        if (string.IsNullOrWhiteSpace(bulkTrackingEventResult.NoRecordFound))
        {
            return Ok(bulkTrackingEventResult.BulkEventTracking);
        }
        else
        {
            return Content(HttpStatusCode.NotFound, "Some Message");
        }
    }
    catch (Exception ex)
    {
        return new ResponseMessageResult(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Some Message"));
    }
}

And my test method is

[TestMethod]
public void BulkTrackEventsByDateRange()
{
    //Given: Username as 'Stamps' and startDate 1 months old date and endDate as yesterday's date
    string username = "Stamps";
    string startDate = DateTime.Now.AddMonths(-1).Date.ToString("MM-dd-yyyy");
    string endDate = DateTime.Now.AddDays(-1).Date.ToString("MM-dd-yyyy");
    // When: I call TrackingEventApiController object (url: /Stamps/09-22-2016/09-22-2016)
    List<BulkEventTrackingRepositoryModel> trackingEvent = new List<BulkEventTrackingRepositoryModel>();
    BulkEventTrackingResultModel trackingEventResult = new BulkEventTrackingResultModel
    {
        ErrorMessage = string.Empty,
        NoRecordFound = "No records found for the given date range.",
        BulkEventTracking = trackingEvent
    };
    _mockBulkTrackingByDateRange.Setup(x => x.GetBulkTrackingEvents(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(trackingEventResult);
    IHttpActionResult actionResult = _trackingEventController.BulkTrackEventsByDateRange(username, startDate, endDate);
    var contentResult = actionResult as NotFoundResult;
    // Then: 
    Assert.IsNotNull(contentResult);
}

But my problem is line

var contentResult = actionResult as NotFoundResult;

does not casting it to NotFoundResult so the output is always null.

How do I fix my test case?

Upvotes: 0

Views: 2231

Answers (1)

Nkosi
Nkosi

Reputation: 247451

The action is not returning a NotFoundResult in that use case but rather NegotiatedContentResult<T>. That is why the cast results in null. The good news is that NegotiatedContentResult<T> has a HttpStatusCode StatusCode { get; } property that can be used to check status of result.

Update test to expect NegotiatedContentResult<string>

//...other code removed for brevity
var contentResult = actionResult as NegotiatedContentResult<string>;
// Then: 
Assert.IsNotNull(contentResult);
Assert.AreEqual(HttpStatusCode.NotFound, contentResult.StatusCode);

and test should exercise as expected.

Upvotes: 1

Related Questions