Unit test bug

Topics: Validation Application Block
Jul 11, 2007 at 4:20 PM
Edited Jul 11, 2007 at 4:28 PM
The file Validation\Validators\RelativeDateTimeValidatorFixture.cs contains the SuppliesAppropriateParametersToMessageTemplate which fails if executed in a machine that has different UICulture and CurrentCulture (my case ;)).
Failure is:

TestCase 'Microsoft.Practices.EnterpriseLibrary.Validation.Tests.Validators.RelativeDateTimeValidatorFixture.SuppliesAppropriateParametersToMessageTemplate'
failed: Assert.AreEqual failed. Expected:<21/06/2007 01:18:52 p.m.>, Actual:<6/21/2007 1:18:52 PM>.

As you can see, the ToString formatting is different.
The key is that the validator is using String.Format using the CurrentUICulture, whereas the test is just using target.ToString(). The fix is to make the target a proper DateTime object, and call target.ToString(CultureInfo.CurrentUICulture)

Fixed test:

		[TestMethod]
		public void SuppliesAppropriateParametersToMessageTemplate()
		{
			RelativeDateTimeValidator validator = new RelativeDateTimeValidator(-10, DateTimeUnit.Day, 20, DateTimeUnit.Year);
			validator.MessageTemplate = "{0}|{1}|{2}|{3}|{4}|{5}|{6}";
			validator.Tag = "tag";
			DateTime target = DateTime.Now.AddDays(-20);
			string key = "key";
 
			ValidationResults validationResults = new ValidationResults();
			validator.DoValidate(target, null, key, validationResults);
 
			Assert.IsFalse(validationResults.IsValid);
			ValidationResult validationResult = ValidationTestHelper.GetResultsList(validationResults)[0];
			System.Text.RegularExpressions.Match match = TemplateStringTester.Match(validator.MessageTemplate, validationResult.Message);
			Assert.IsTrue(match.Success);
			Assert.IsTrue(match.Groups["param0"].Success);
			Assert.AreEqual(target.ToString(CultureInfo.CurrentUICulture), match.Groups["param0"].Value);
			Assert.IsTrue(match.Groups["param1"].Success);
			Assert.AreEqual(key, match.Groups["param1"].Value);
			Assert.IsTrue(match.Groups["param2"].Success);
			Assert.AreEqual(validator.Tag, match.Groups["param2"].Value);
			Assert.IsTrue(match.Groups["param3"].Success);
			Assert.AreEqual("-10", match.Groups["param3"].Value);
			Assert.IsTrue(match.Groups["param4"].Success);
			Assert.AreEqual("Day", match.Groups["param4"].Value);
			Assert.IsTrue(match.Groups["param5"].Success);
			Assert.AreEqual("20", match.Groups["param5"].Value);
			Assert.IsTrue(match.Groups["param6"].Success);
			Assert.AreEqual("Year", match.Groups["param6"].Value);
		}