Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lfreneda/4059466 to your computer and use it in GitHub Desktop.
Save lfreneda/4059466 to your computer and use it in GitHub Desktop.
Base Class for Controller tests
public class SimpleControllerTests {
public void EnsureActionViewName<TController>(Func<TController, ActionResult> executeAction, string viewNameExpected)
where TController : Controller {
var controller = Activator.CreateInstance<TController>();
var viewResult = executeAction.Invoke(controller) as ViewResultBase;
AssertViewName(viewNameExpected, viewResult);
}
public void EnsureActionViewName(Func<ActionResult> executeAction, string viewNameExpected) {
var viewResult = executeAction.Invoke() as ViewResultBase;
AssertViewName(viewNameExpected, viewResult);
}
public void EnsureModelType<TController>(Func<TController, ActionResult> executeAction, Type modelTypeExpected) {
var controller = Activator.CreateInstance<TController>();
var viewResult = executeAction.Invoke(controller) as ViewResultBase;
AssertType(modelTypeExpected, viewResult);
}
public void EnsureModelType(Func<ActionResult> executeAction, Type modelTypeExpected) {
var viewResult = executeAction.Invoke() as ViewResultBase;
AssertType(modelTypeExpected, viewResult);
}
private static void AssertType(Type modelTypeExpected, ViewResultBase viewResult) {
viewResult.Model.GetType().Should().Be.EqualTo(modelTypeExpected);
}
private static void AssertViewName(string viewNameExpected, ViewResultBase viewResult) {
viewResult.ViewName.Should().Be(viewNameExpected);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment