我在控制器中有以下操作,该操作会抛出 HttpException ,状态代码为 404 :
public async Task<ActionResult> Edit(int id)
{
Project proj = await _service.GetProjectById(id);
if( proj == null)
{
throw new HttpException(404, "Project not found.");
}
}
为了测试这个场景,我编写了下面的测试用例,其中我捕获了AggregationException并重新抛出了InnerException,它被期望为HttpException:
[TestMethod]
[ExpectedException(typeof(HttpException),"Project not found.")]
public void Edit_Project_Load_InCorrect_Value()
{
Task<ActionResult> task = _projectController.Edit(3);
try
{
ViewResult result = task.Result as ViewResult;
Assert.AreEqual("NotFound", result.ViewName, "Incorrect Page title");
}
catch (AggregateException ex)
{
throw ex.InnerException;
}
}
此测试成功运行并返回ExpectedException。我在这里有两个问题:
答案 0 :(得分:1)
有一种更好的方法来测试它。我们编写了一个名为AssertHelpers.cs
的类,其中包含此方法。这比ExpectedException
更好的原因是ExpectedException
实际上没有验证它被抛出,它只是允许测试在抛出时通过。
例如,如果您将404代码更改为200,则测试不会失败。
public static void RaisesException<TException>(Action dataFunction, string exceptionIdentifier = null)
{
bool threwException = false;
try
{
dataFunction();
}
catch (Exception e)
{
threwException = true;
Assert.IsInstanceOfType(e, typeof(TException));
if (exceptionIdentifier != null)
Assert.AreEqual(exceptionIdentifier, e.Message);
}
if (!threwException)
Assert.Fail("Expected action to raise exception with message: " + exceptionIdentifier);
}