Posted By

RockinForGod on 07/21/09


Tagged

aspnet-mvc action-filter unit-testing


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

umang_nine


ASP.NET MVC - ValidateAntiSpamAtrribute Test Class


 / Published in: C#
 

  1. [TestClass]
  2. public class ValidateAntiSpamAttributeTest
  3. {
  4. /// <summary>
  5. ///Gets or sets the test context which provides
  6. ///information about and functionality for the current test run.
  7. ///</summary>
  8. public TestContext TestContext { get; set; }
  9.  
  10. #region Additional test attributes
  11. //
  12. // You can use the following additional attributes as you write your tests:
  13. //
  14. // Use ClassInitialize to run code before running the first test in the class
  15. // [ClassInitialize()]
  16. // public static void MyClassInitialize(TestContext testContext) { }
  17. //
  18. // Use ClassCleanup to run code after all tests in a class have run
  19. // [ClassCleanup()]
  20. // public static void MyClassCleanup() { }
  21. //
  22. // Use TestInitialize to run code before running each test
  23. // [TestInitialize()]
  24. // public void MyTestInitialize() { }
  25. //
  26. // Use TestCleanup to run code after each test has run
  27. // [TestCleanup()]
  28. // public void MyTestCleanup() { }
  29. //
  30. #endregion
  31.  
  32. private ActionExecutingContext GetFilterContext(string sixtimesnine, string fourtytwo)
  33. {
  34. var formCollection = new NameValueCollection();
  35. formCollection.Add("sixtimesnine", sixtimesnine);
  36. formCollection.Add("fourtytwo", fourtytwo);
  37. var httpRequest = new Mock<HttpRequestBase>();
  38. httpRequest.Expect(r => r.Form).Returns(formCollection);
  39. var httpContext = new Mock<HttpContextBase>();
  40. httpContext.Expect(hc => hc.Request).Returns(httpRequest.Object);
  41. var actionDescriptor = new Mock<ActionDescriptor>().Object;
  42. var controller = new FakeController();
  43. var controllerContext =
  44. new ControllerContext(httpContext.Object, new RouteData(), controller);
  45. return
  46. new ActionExecutingContext(controllerContext, actionDescriptor, new RouteValueDictionary());
  47. }
  48.  
  49. [TestMethod]
  50. public void OnActionExecuting_FormValuesEqual_NoErrorsAdded()
  51. {
  52. // Arrange
  53. var attribute = new ValidateAntiSpamAttribute();
  54. var filterContext = this.GetFilterContext("nej", "nej");
  55.  
  56. // Act
  57. attribute.OnActionExecuting(filterContext);
  58.  
  59. // Assert
  60. var modelState = filterContext.Controller.ViewData.ModelState;
  61. Assert.AreEqual(0, modelState.Count);
  62. Assert.AreEqual("passed", (string)filterContext.Controller.ViewData["filter"]);
  63. }
  64.  
  65. [TestMethod]
  66. public void OnActionExecuting_FormValuesNotEqual_ErrorAddedToModelState()
  67. {
  68. // Arrange
  69. var attribute = new ValidateAntiSpamAttribute("test message");
  70. var filtercontext = this.GetFilterContext("nej", "ja");
  71.  
  72. // Act
  73. attribute.OnActionExecuting(filtercontext);
  74.  
  75. // Assert
  76. var modelState = filtercontext.Controller.ViewData.ModelState;
  77. Assert.IsTrue(modelState.Count > 0);
  78. Assert.AreEqual(modelState["Spam"].Errors[0].ErrorMessage, "test message");
  79. }
  80.  
  81. [TestMethod]
  82. public void OnActionExecuting_NoFormValues_ErrorAddedToModelState()
  83. {
  84. // Arrange
  85. var attribute = new ValidateAntiSpamAttribute("test message");
  86. var filtercontext = this.GetFilterContext(string.Empty, string.Empty);
  87.  
  88. // Act
  89. attribute.OnActionExecuting(filtercontext);
  90.  
  91. // Assert
  92. var modelState = filtercontext.Controller.ViewData.ModelState;
  93. Assert.IsTrue(modelState.Count > 0);
  94. Assert.AreEqual(modelState["Spam"].Errors[0].ErrorMessage, "test message");
  95. }
  96. }

Report this snippet  

You need to login to post a comment.