From: Joe W. <joe...@us...> - 2002-10-07 19:32:08
|
Update of /cvsroot/mockobjects/nmock/sample/order In directory usw-pr-cvs1:/tmp/cvs-serv25969/sample/order Added Files: Notifier.cs Order.cs OrderProcessor.cs OrderProcessorTest.cs Log Message: Another NMock sample added. --- NEW FILE: Notifier.cs --- using System.Web.Mail; namespace NMockSample.Order { /// <summary> /// Notify people of events using SMTP emails. /// </summary> public class Notifier { private string from = "nobody"; private string admin = "admin"; public virtual void NotifyAdmin(string msg) { MailMessage mail = new MailMessage(); mail.From = from; mail.To = admin; mail.Subject = msg; mail.Body = msg; SmtpMail.Send(mail); } public virtual void NotifyUser(string user, string msg) { MailMessage mail = new MailMessage(); mail.From = from; mail.To = user; mail.Subject = msg; mail.Body = msg; SmtpMail.Send(mail); } } } --- NEW FILE: Order.cs --- using System.Data; using System.Data.SqlClient; namespace NMockSample.Order { /// <summary> /// Details of Order stored directly in database. /// </summary> public class Order { private DataRow row; public virtual int Number { get { return (int)row["number"]; } } public virtual double Amount { get { return (double)row["amount"]; } } public virtual bool Urgent { get { return (bool)row["urgent"]; } } public virtual string User { get { return (string)row["user"]; } } public virtual void Load(SqlConnection con, int id) { string sql = "SELECT * FROM orders WHERE id = " + id; using (SqlDataAdapter adapter = new SqlDataAdapter(sql, con)) { DataTable table = new DataTable(); adapter.Fill(table); row = table.Rows[0]; } } } } --- NEW FILE: OrderProcessor.cs --- using System; namespace NMockSample.Order { /// <summary> /// Process an order: /// - If the amount is greater than or equal to 1000, notify the user that /// their order has been dispatched. /// - If the order is also marked as urgent, notify the administrator to urgently dispatch. /// - If the order is less than 1000, be quiet. It's not worth our time. /// </summary> public class OrderProcessor { internal Notifier notifier = new Notifier(); public virtual void Process(Order order) { if (order.Amount >= 1000) { notifier.NotifyUser(order.User, String.Format( "Order {0} has been dispatched", order.Number)); if (order.Urgent) { notifier.NotifyAdmin(String.Format("Order {0} needs to be urgently dispatched to {1}", order.Number, order.User)); } } } } } --- NEW FILE: OrderProcessorTest.cs --- using NUnit.Framework; using NMock; namespace NMockSample.Order { /// <summary> /// This test demonstrates how to test a class interacts with other domain specific /// classes properly. /// /// The Order class is coupled to the database. The Notifier is coupled to the SMTP mail /// system. This fixture is testing the OrderProcessor which directly interacts with /// Order and Notifier. /// /// The test substitutes in mock implementations of Order and Notifier so OrderProcessor /// can be tested in isolation. /// </summary> [TestFixture] public class OrderProcessorTest { private IMock order; private IMock notifier; private OrderProcessor orderProcessor; [SetUp] public void SetUp() { // setup mock Order and populate with default return values. order = new DynamicMock(typeof(Order)); order.SetValue("Amount", 1002.0); order.SetValue("Urgent", false); order.SetValue("Number", 123); order.SetValue("User", "joe"); // create mock Notifier notifier = new DynamicMock(typeof(Notifier)); // create real OrderProcessor to be tested orderProcessor = new OrderProcessor(); // switch the OrderProcessor to use the mock Notifier orderProcessor.notifier = (Notifier)notifier.Object; } [Test] public void NotifyUser() { // setup notifier.Expect("NotifyUser", "joe", "Order 123 has been dispatched"); // execute orderProcessor.Process((Order)order.Object); // verify notifier.Verify(); } [Test] public void NotifyAnotherUserAnotherNumber() { // setup order.SetValue("Number", 456); order.SetValue("User", "chris"); notifier.Expect("NotifyUser", "chris", "Order 456 has been dispatched"); // execute orderProcessor.Process((Order)order.Object); // verify notifier.Verify(); } [Test] public void DontNotifyUser() { // setup order.SetValue("Amount", 999.0); // execute orderProcessor.Process((Order)order.Object); // verify notifier.Verify(); } [Test] public void NotifyUserAndAdmin() { // setuo order.SetValue("Urgent", true); notifier.Expect("NotifyUser", "joe", "Order 123 has been dispatched"); notifier.Expect("NotifyAdmin", "Order 123 needs to be urgently dispatched to joe"); // execute orderProcessor.Process((Order)order.Object); // verify notifier.Verify(); } [Test] public void DontNotifyEvenThoughUrgent() { // setup order.SetValue("Amount", 999.0); order.SetValue("Urgent", true); // execute orderProcessor.Process((Order)order.Object); // verify notifier.Verify(); } } } |