ice7

Mock Objects

Aug 26th, 2011
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1. /* The Test - Taken from 'http://dimecasts.net/Content/WatchEpisode/12' and re-written in 'AAA' syntax from Record and Playback. */
  2.     [TestClass]
  3.     public class UnitTest1
  4.     {
  5.         [TestMethod]
  6.         public void EmailerServiceTest()
  7.         {
  8.             //Arrange
  9.             var emailerService = MockRepository.GenerateMock<IEmailerService>();
  10.             emailerService.Expect(x => x.SendEmail("", "")).IgnoreArguments().Return(true).Repeat.Any();
  11.  
  12.             //Act
  13.             var emailer = new Emailer(emailerService);
  14.             emailer.SendBatchEmails();
  15.  
  16.             //Assert                                                 Call ignore arguments becasue you don't care what they are
  17.             emailerService.AssertWasCalled(x => x.SendEmail("", ""), y => y.IgnoreArguments());
  18.             emailerService.VerifyAllExpectations();
  19.         }
  20.  
  21.  
  22.         [TestMethod, ExpectedException(typeof(Exception))]
  23.         public void EmailerServiceTestException()
  24.         {
  25.  
  26.             //Arrange
  27.             var emailerService = MockRepository.GenerateMock<IEmailerService>();
  28.             emailerService.Expect(x => x.SendEmail("", "")).IgnoreArguments().Return(false);
  29.  
  30.             //Act
  31.             var emailer = new Emailer(emailerService);
  32.             emailer.SendBatchEmails();
  33.             //emailerService.AssertWasNotCalled(x => x.SendEmail("", ""), y => y.IgnoreArguments());
  34.         }
  35.     }
  36.  
  37. //The Class
  38.  
  39. using System;
  40. using System.Collections.Generic;
  41. using System.Linq;
  42. using System.Text;
  43.  
  44. namespace TestLib
  45. {
  46.  
  47.     public interface IEmailerService
  48.     {
  49.         bool SendEmail(string email, string message);
  50.     }
  51.  
  52.     public class EmailerService
  53.     {
  54.         public bool SendEmail(string email, string message)
  55.         {
  56.             return false;
  57.         }
  58.     }
  59.  
  60.     public class Emailer
  61.     {
  62.         private IEmailerService _emailerService;
  63.         public Emailer(IEmailerService emailerService)
  64.         {
  65.             this._emailerService = emailerService;
  66.         }
  67.  
  68.         public void SendBatchEmails()
  69.         {
  70.             var emails = new Dictionary<string, string>
  71.                             {
  72.                                 {"[email protected]", "Hello1"},
  73.                                 {"[email protected]", "Hello2"},
  74.                                 {"[email protected]", "Hello3"},
  75.                                 {"[email protected]", "Hello4"}
  76.                             };
  77.             foreach(KeyValuePair<string, string> email in emails)
  78.             {
  79.                 if(!_emailerService.SendEmail(email.Key, email.Value))
  80.                 {
  81.                     throw new Exception(" You've Err'd");
  82.                 }
  83.             }
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment