Reputation: 379
I am following TDD approach to develop my MVC website. I have a PaymentController
which is going to have an action method MakePayment
which I am testing using a test method as given below:
[TestMethod]
public void MakePaymentLoad()
{
PaymentController payController = new PaymentController();
ViewResult payResult = payController.MakePayment() as ViewResult;
Assert.IsNotNull(payResult);
}
[TestMethod]
public void MakePaymentResult()
{
PaymentController payController = new PaymentController();
Payment payment = new Payment {
BillerId = 1,
PayAmt = 1.0,
PayDt = DateTime.Now,
ConfCode = null,
BillAccount = "123",
PayStatus = 1,
FeeStatus = 1,
Platform =1
};
ViewResult payResult = payController.MakePayment(payment) as ViewResult;
PaymentResult result = payResult.Model as PaymentResult;
Assert.IsNotNull(result.ConfCode);
}
In the above given test methods MakePaymentLoad
only checks if the view is rendered and MakePaymentResult
checks out if the confirmation code is present in the view model.
My action methods are given below:
[HttpPost]
public ActionResult MakePayment(Payment payment)
{
PaymentResult payResult = new PaymentResult {
ConfCode = "123"
};
if (true)
{
TempData["ConfCode"] = "123";
return RedirectToAction("Confirmation");
}
return View(payment);
}
public ViewResult MakePayment()
{
return View();
}
public ActionResult Confirmation()
{
PaymentResult result = new PaymentResult {
ConfCode = Convert.ToString(TempData["ConfCode"])
};
return View(result);
}
The MakePaymentLoad
passes as it only check if the view is rendered whereas MakePaymentResult
fails miserably as the result of action method is null because of the use RedirectToAcion
inside MakePayment's
post version. Please let me know how to fix this.
Upvotes: 0
Views: 652
Reputation: 5989
you should test it like following
var payResult = (RedirectToActionResult)payController.MakePayment(payment)
Assert.AreEqual("Confirmation", action.RouteValues["action"]);
As you are returning redirect result, you can't expect a model in return.
Upvotes: 2