[TestFixture] public class AccountTests { private MockMailServer mockMailServer = new MockMailServer(); [Test] public void WhoAreYouReceivingTheMostEmailFrom() { User you = User.CreateNew( "you" ); User mike = SendEmailHelper( CreateUser( "mike" ), you, 3 ); User mary = SendEmailHelper( CreateUser( "mary" ), you, 4 ); User joan = SendEmailHelper( CreateUser( "joan" ), you, 2 ); Assert.AreEqual( mary, you.GetGreatestEmailer() ); } private User SendEmailHelper(User from, User to, int quantity) { for ( int i = 0; i < quantity; i++ ) { EMail mail = new EMail() { To = to, From = from, Body = "Sample", Subject = "test" }; mail.SetFormat( Formats.Html ); mockMailServer.Send( mail ); } return from; }
[Test] public void WhoAreYouReceivingTheMostEmailFrom() { User you = User.CreateNew("you"); User mike = CreateUser("mike"); for (int i = 0; i < 3; i++) { EMail mail = new EMail{To = you,From = mike, Body = "Sample", Subject = "test"}; mail.SetFormat(Formats.Html); mockMailServer.Send(mail); } User mary = CreateUser("mary"); for (int i = 0; i < 4; i++) { EMail mail1 = new EMail{To = you,From = mary,Body = "Sample",Subject = "test"}; mail1.SetFormat(Formats.Html); mockMailServer.Send(mail1); } User joan = CreateUser("joan"); for (int i = 0; i < 6; i++) { EMail mail2 = new EMail {To = you, From = joan,Body = "Sample",Subject = "test"}; mail2.SetFormat(Formats.Html); mockMailServer.Send(mail2); } Assert.AreEqual(mary, you.GetGreatestEmailer()); }
[Test] public void TestGatewayCallSuccessful() { var gateway = new Gateway {Mask = "ExampleCode.*"}; var enviroment = new Dictionary(); enviroment.Add("path", "ExampleCode.HelloWorld"); string result = gateway.ExecuteRequest(enviroment); Assert.IsTrue(result.Contains("Hello World")); } [Test] public void TestGatewayBlocksInvalidMasks() { Assert.IsFalse(Gateway.IsValidForMask( "Example.*", "ExampleCode.HelloWorld")); Assert.IsFalse(Gateway.IsValidForMask( "ExampleCode.*.Extras.*", "ExampleCode.HelloWorld")); Assert.IsTrue(Gateway.IsValidForMask( "ExampleCode.*", "ExampleCode.HelloWorld")); }
public class Gateway : IRunner { public string Mask { get; set; } public String ExecuteRequest(Dictionary environment) { string path = environment["path"]; AssertValidClass(path); IRunner instance = (IRunner)Activator.CreateInstance(Type.GetType(path)); return instance.ExecuteRequest(environment); } private void AssertValidClass(string path) { if (!IsValidForMask(Mask, path)) { throw new Exception(String.Format( "Invalid Path '{0}' for Mask '{1}'", path, Mask)); } } internal static bool IsValidForMask(String mask, String path) { mask = mask.Replace(".", "\\.").Replace("*", ".*"); Regex regex = new Regex(mask); return regex.IsMatch(path); } }
public class Gateway : IRunner { private string mask; private Regex regex; public string Mask { get { return mask; } set { mask = value; regex = new Regex( mask.Replace( ".", "\\." ).Replace( "*", ".*" ) ); } } public String ExecuteRequest(Dictionary environment) { string path = environment["path"]; AssertValidClass(path); IRunner instance = (IRunner) Activator.CreateInstance(Type.GetType(path)); return instance.ExecuteRequest(environment); } private void AssertValidClass(string path) { if (!regex.IsMatch(path)) { throw new Exception(String.Format("Invalid Path '{0}' for Mask '{1}'", path, Mask)); } } }
[TestFixture] public class GatewayBehaviorTests { [Test] private void TestGatewayCallSuccessful() { string result = Run("ExampleCode.*", "ExampleCode.HelloWorld"); Assert.IsTrue(result.Contains("Hello World")); } [Test] public void TestGatewayBlocksInvalidMasks() { AssertValidForMask(false, "Example.*", "ExampleCode.HelloWorld"); AssertValidForMask(false, "ExampleCode.*.Extras.*", "ExampleCode.HelloWorld"); AssertValidForMask(true, "ExampleCode.*", "ExampleCode.HelloWorld"); } private static String Run(string mask, string path) { var gateway = new OptimizedGateway {Mask = mask}; var enviroment = new Dictionary(); enviroment.Add("path", path); string result = gateway.ExecuteRequest(enviroment); return result; } private void AssertValidForMask( bool exceptionExpected, string mask, string path) { Exception found = null; try { Run(mask, path); } catch (Exception ex) { found = ex; } Assert.AreEqual(exceptionExpected, found == null); } }
public int doSomething(int a, int b) {/*...*/})
doSomething(2,3);
assertEquals(8, doSomething(2,3));
Posted in Articles | DevelopMentor | Unit Testing
A blog by Michael Kennedy
About: Michael is an instructor for DevelopMentor, a .NET enthusiast, an agile pioneer, an entrepreneur, a father of three girls, a husband, a student, and a teacher.
Explore Related Content
Concepts >> azure - Workflow - RESTful - Unit Testing - web - WCF - ASP.Net - MVC - LINQ
Tools >> Visual Studio ClickOnce IIS
Type >> Screencast - Tools - Article - How To - Course
@DevelopMentor_ @mkennedy @andrewclymer @bmaso @coloringinguy @danamiga @ellisteam1 @georgeshep @isidore_us @jason_diamond @markblomsma @marksm @mauricedb @mscottreed @pierrenallet @richardblewett @tonysneed @wallacekelly
Sign In