I've been reading a lot on TDD over the past few months and decided to jump in and try it out with an easy example, I'm just not sure I'm testing for the right things in practice. Here the tests for a custom Data Annotation for validating emails:
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MembershipTest.Tests
{
[TestClass]
public class CustomDataAnnotationsTest
{
[TestMethod]
public void CustomDataAnnotations_Email_ReturnTrueIfNull()
{
// Arrange
EmailAttribute attribute = new EmailAttribute();
// Act
bool result = attribute.IsValid(null);
// Assert
Assert.AreEqual(true, result);
}
[TestMethod]
public void CustomDataAnnotations_Email_ReturnFalseIfInvalid()
{
// Arrange
EmailAttribute attribute = new EmailAttribute();
// Act
bool result = attribute.IsValid("()[]\\;:,<>@example.com");
// Assert
Assert.AreEqual(false, result);
}
[TestMethod]
public void CustomDataAnnotations_Email_ReturnTrueIfValid()
{
// Arrange
EmailAttribute attribute = new EmailAttribute();
// Act
bool result = attribute.IsValid("[email protected]");
// Assert
Assert.AreEqual(true, result);
}
}
}
And here is the subsequent code written for the test:
using System;
using System.ComponentModel.DataAnnotations;
using System.Net.Mail;
public class EmailAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
//Let RequiredAttribute validate whether the value is required or not.
if (value == null)
{
return true;
}
//Check to see if System.Net.Mail can send to the address.
try
{
var i = new MailAddress(value.ToString());
}
catch (Exception)
{
return false;
}
return true;
}
}
All tests failed initially and then succeeded after writing the code, but are the tests appropriately written? Too much, or too little? I know this is a very simple example, but I want to make sure I'm on the right track before moving on to more complicated things.