Return to Snippet

Revision: 15942
at July 21, 2009 09:55 by RockinForGod


Initial Code
[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)
		{
			var formCollection = new NameValueCollection();
			formCollection.Add("sixtimesnine", sixtimesnine);
			formCollection.Add("fourtytwo", fourtytwo);
			var httpRequest = new Mock<HttpRequestBase>();
			httpRequest.Expect(r => r.Form).Returns(formCollection);
			var httpContext = new Mock<HttpContextBase>();
			httpContext.Expect(hc => hc.Request).Returns(httpRequest.Object);
			var actionDescriptor = new Mock<ActionDescriptor>().Object;
			var controller = new FakeController();
			var controllerContext =
				new ControllerContext(httpContext.Object, new RouteData(), controller);
			return 
				new ActionExecutingContext(controllerContext, actionDescriptor, new RouteValueDictionary());
		}

		[TestMethod]
		public void OnActionExecuting_FormValuesEqual_NoErrorsAdded()
		{
			// Arrange
			var attribute = new ValidateAntiSpamAttribute();
			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 attribute = new ValidateAntiSpamAttribute("test message");
			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 attribute = new ValidateAntiSpamAttribute("test message");
			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");
		}
	}

Initial URL

                                

Initial Description

                                

Initial Title
ASP.NET MVC - ValidateAntiSpamAtrribute Test Class

Initial Tags

                                

Initial Language
C#