/ Published in: C#
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
[TestClass] public class ValidateAntiSpamAttributeTest { /// <summary> ///Gets or sets the test context which provides ///information about and functionality for the current test run. ///</summary> public TestContext TestContext { get; set; } #region Additional test attributes // // You can use the following additional attributes as you write your tests: // // Use ClassInitialize to run code before running the first test in the class // [ClassInitialize()] // public static void MyClassInitialize(TestContext testContext) { } // // Use ClassCleanup to run code after all tests in a class have run // [ClassCleanup()] // public static void MyClassCleanup() { } // // Use TestInitialize to run code before running each test // [TestInitialize()] // public void MyTestInitialize() { } // // Use TestCleanup to run code after each test has run // [TestCleanup()] // public void MyTestCleanup() { } // #endregion private ActionExecutingContext GetFilterContext(string sixtimesnine, string fourtytwo) { formCollection.Add("sixtimesnine", sixtimesnine); formCollection.Add("fourtytwo", fourtytwo); httpRequest.Expect(r => r.Form).Returns(formCollection); httpContext.Expect(hc => hc.Request).Returns(httpRequest.Object); var controllerContext = return } [TestMethod] public void OnActionExecuting_FormValuesEqual_NoErrorsAdded() { // Arrange var filterContext = this.GetFilterContext("nej", "nej"); // Act attribute.OnActionExecuting(filterContext); // Assert var modelState = filterContext.Controller.ViewData.ModelState; Assert.AreEqual(0, modelState.Count); Assert.AreEqual("passed", (string)filterContext.Controller.ViewData["filter"]); } [TestMethod] public void OnActionExecuting_FormValuesNotEqual_ErrorAddedToModelState() { // Arrange var filtercontext = this.GetFilterContext("nej", "ja"); // Act attribute.OnActionExecuting(filtercontext); // Assert var modelState = filtercontext.Controller.ViewData.ModelState; Assert.IsTrue(modelState.Count > 0); Assert.AreEqual(modelState["Spam"].Errors[0].ErrorMessage, "test message"); } [TestMethod] public void OnActionExecuting_NoFormValues_ErrorAddedToModelState() { // Arrange var filtercontext = this.GetFilterContext(string.Empty, string.Empty); // Act attribute.OnActionExecuting(filtercontext); // Assert var modelState = filtercontext.Controller.ViewData.ModelState; Assert.IsTrue(modelState.Count > 0); Assert.AreEqual(modelState["Spam"].Errors[0].ErrorMessage, "test message"); } }