2016-11-23 10 views
0

私のアクションメソッドが404ステータスコードを返すかどうかを確認するテストケースを作成しようとしていました。asp.net web apiでNotFound http statusをテストする方法

だからここに私のウェブアピアクションメソッド

[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")); 
    } 
} 

そして、私の試験方法は、

[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); 
} 

あるしかし、私の問題はNotFoundResultにそれをキャストしないライン

var contentResult = actionResult as NotFoundResult; 

です出力は常にnullです。

テストケースを修正するにはどうすればよいですか?

+0

ExecuteAsyncを使用し、受信したオブジェクトのHttpResponseMessage.StatusCodeを確認する必要があります。 https://msdn.microsoft.com/en-us/library/system.web.http.ihttpactionresult_methods(v=vs.118).aspx https://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage.statuscode(v = 118).aspx < –

答えて

0

この操作ではNotFoundResultが返されず、その代わりにNegotiatedContentResult<T>が返されます。そのためキャストはnullになります。良いニュースは、NegotiatedContentResult<T>には結果のステータスを確認するために使用できるHttpStatusCode StatusCode { get; }プロパティがあることです。予想通りNegotiatedContentResult<string>

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

とテストを期待する

更新テストが行​​使すべきです。

関連する問題