我目前正在单元测试中,当发送无效的表单收集数据时会引发错误。
异常在HttpPost Index ActionResult方法中引发,如下所示:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(FormCollection formCollection, PaymentType payType, string progCode)
{
ActionResult ar = redirectFromButtonData(formCollection, payType, progCode);
if (ar != null)
{
return ar;
}
else
{
throw new Exception("Cannot redirect to payment form from cohort decision - Type:[" + payType.ToString() + "] Prog:[" + Microsoft.Security.Application.Encoder.HtmlEncode(progCode) + "]");
}
}
到目前为止,我已经编写了一个成功击中该异常的测试(我已经通过启用代码覆盖率来验证这一点,我已经使用该代码覆盖率来查看每个测试正在执行的代码),但是当前该测试失败了,因为我尚未定义一种测试是否引发异常的方法,可以在下面找到该测试的代码:
[TestMethod]
public void Error_Is_Thrown_If_HVM_FormCollection_Data_Is_Incorrect()
{
var formCollection = new FormCollection();
formCollection.Add("__RequestVerificationToken", "__RequestVerificationToken");
formCollection.Add("invalid - invalid", "invalid- invalid");
var payType = new PaymentType();
payType = PaymentType.deposit;
var progCode = "hvm";
var mocks = new MockRepository();
var httpRequest = mocks.DynamicMock<HttpRequestBase>();
var httpContext = mocks.DynamicMock<HttpContextBase>();
controller.ControllerContext = new ControllerContext(httpContext, new RouteData(), controller);
mocks.ReplayAll();
httpRequest.Expect(r => r.Url).Return(new Uri("http://localhost:8080/hvm/full/self/")).Repeat.Any();
httpContext.Expect(c => c.Request).Return(httpRequest).Repeat.Any();
var result = controller.Index(formCollection, payType, progCode);
}
我已经看过使用
[ExpectedException(typeof(Exception)]
注释可以在这种情况下使用吗? 最佳答案
我自由地更改了您的测试代码,使其略微符合犀牛的最新功能。不再需要创建MockRepository
,可以使用静态类MockRepository
并调用GenerateMock<>
。我还将您的SuT(测试中的系统)实例化移动到了针对您模拟对象的规范之下。
Nunit的示例(我对Nunit的体验比对MSTest的体验更好。主要是因为Nunit的发布频率更高,并且具有更可靠的功能集。同样,不确定它是否可以与TFS一起使用,但是应该不难发现)。
[Test] // Nunit
[ExpectedException(typeof(Exception)) // NOTE: it's wise to throw specific
// exceptions so that you prevent false-positives! (another "exception"
// might make the test pass while it's a completely different scenario)
public void Error_Is_Thrown_If_HVM_FormCollection_Data_Is_Incorrect()
{
var formCollection = new FormCollection();
formCollection.Add("__RequestVerificationToken", "__RequestVerificationToken");
formCollection.Add("invalid - invalid", "invalid- invalid");
var payType = new PaymentType();
payType = PaymentType.deposit;
var progCode = "hvm";
var httpRequest = MockRepository.GenerateMock<HttpRequestBase>();
var httpContext = MockRepository.GenerateMock<HttpContextBase>();
// define behaviour
httpRequest.Expect(r => r.Url).Return(new Uri("http://localhost:8080/hvm/full/self/")).Repeat.Any();
httpContext.Expect(c => c.Request).Return(httpRequest).Repeat.Any();
// instantiate SuT (system under test)
controller.ControllerContext = new ControllerContext(httpContext, new RouteData(), controller);
// Test the stuff, and if nothing is thrown then the test fails
var result = controller.Index(formCollection, payType, progCode);
}
与MStest几乎相同,所不同的是,您需要定义期望的异常,但需要多一些旧知识。
[TestMethod] // MStest
public void Error_Is_Thrown_If_HVM_FormCollection_Data_Is_Incorrect()
{
try
{
var formCollection = new FormCollection();
formCollection.Add("__RequestVerificationToken", "__RequestVerificationToken");
formCollection.Add("invalid - invalid", "invalid- invalid");
var payType = new PaymentType();
payType = PaymentType.deposit;
var progCode = "hvm";
var httpRequest = MockRepository.GenerateMock<HttpRequestBase>();
var httpContext = MockRepository.GenerateMock<HttpContextBase>();
// define behaviour
httpRequest.Expect(r => r.Url).Return(new Uri("http://localhost:8080/hvm/full/self/")).Repeat.Any();
httpContext.Expect(c => c.Request).Return(httpRequest).Repeat.Any();
// instantiate SuT (system under test)
controller.ControllerContext = new ControllerContext(httpContext, new RouteData(), controller);
// Test the stuff, and if nothing is thrown then the test fails
var result = controller.Index(formCollection, payType, progCode);
}
catch (Exception)
{
Assert.Pass();
}
Assert.Fail("Expected exception Exception, was not thrown");
}
如果该部分正常工作,则可以通过提供的链接进行重构,以实现更好的可重用性:Assert exception from NUnit to MS TEST。