I want to unit test the following ASP.NET MVC controller Index action. What do I replace the actual parameter in the assert below (stubbed with ?).
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
public class StatusController : Controller
{
public ActionResult Index()
{
return Content("Hello World!");
}
}
}
[TestMethod]
public void TestMethod1()
{
// Arrange
var controller = CreateStatusController();
// Act
var result = controller.Index();
// Assert
Assert.AreEqual( "Hello World!.", ? );
}
use the "as" operator to make a nullable cast. Then simply check for a null result
I like creating assertion helpers for this sort of thing. For instance, you might do something like:
You'd then call this like:
I think this makes the tests so much easier to read and write.
You cant test that the result is not null, that you receive a
ContentResult
and compare the values:I apoligize if the Nunit asserts aren't welformed, but look at it as pseudo-code :)