我有一个简单的集成测试。
[TestMethod]
public async Task INT_GetSomething_Sucess()
{
//Arrange
HttpResponseMessage response;
BidMonthDetails returnedObj;
//Act
try
{
string request = JsonConvert.SerializeObject(DateTime.Now);
response = await TestClient.PostAsync("/api/Trade/GetSomething", new StringContent(request, Encoding.UTF8, "application/json"));
var jsonString = await response.Content.ReadAsStringAsync();
returnedObj = JsonConvert.DeserializeObject<MyModel>(jsonString);
}
catch (Exception ex)
{
throw ex;
}
//Assert
Assert.AreEqual(response.StatusCode, System.Net.HttpStatusCode.OK);
Assert.IsNotNull(returnedObj);
}
此测试在贸易控制器中称为GetSomething的功能
[HttpPost]
public async Task<ActionResult<MyModel>> GetSomething(DateTime date)
{
在这一点上,“日期”仅是最小日期。我希望它是今天的日期。
答案 0 :(得分:1)
默认情况下,假设DateTime
参数来自查询字符串。由于您没有date
查询字符串参数,因此它默认为DateTime
的默认值DateTime.Min
要解决此问题,您有两种选择:
1)用FromBody属性装饰参数:
public async Task<ActionResult<MyModel>> GetSomething([FromBody]DateTime date)
2)将值放在查询字符串而不是正文中:
response = await TestClient.PostAsync($"/api/Trade/GetSomething?date={DateTime.UtcNow}"...