내 끝점은 다음과 같습니다.
public IHttpActionResult Work()
{
try
{
this.Service.DoWork();
return this.Ok();
}
catch (SomeException)
{
return this.Content(HttpStatusCode.Conflict, new { Message = "The message" });
}
}
오류 메시지를 어떻게 단위 테스트 할 수 있습니까?
다음은 테스트 템플릿입니다.
[Test]
public void Work_Conflict()
{
this.Service.Setup(x => x.DoWork()).Throws<SomeException>();
var result = (<what goes here?>)this.MyController.Work();
Assert.AreEqual("The message", <what goes here?>);
}
거의 다 왔습니다.
결과를 ApiController에서 반환 된 실제 유형으로 캐스팅하고 예상 데이터를 추출해야합니다.
[Test]
public void Work_Conflict()
{
this.Service.Setup(x => x.DoWork()).Throws<SomeException>();
IHttpActionResult result = this.MyController.Work();
var objectResult = result as ObjectResult;
Assert.IsNotNull(objectResult);
dynamic model = objectResult.Value;
string actual = (string)model.Message;
string expected = "The message";
Assert.AreEqual(expected, actual);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다