DevPinoy.org
A Filipino Developers Community
   
Sample NMock code
Okay, I'm posting here my sample code that uses NMock to dynamically create mock objects from interface declarations. It's a very simple system that provides a ForexConverter class. This ForexConverter class performs conversion of money from one currency to another and defers the retrieval of the exchange rate to any implementation of the IExchangeRateRetriever interface.

Here are the stuff...

TestForexConverter.cs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using NUnit.Framework;
using NMock2;

namespace MockDemo1
{
  [TestFixture]
  public class TestForexConverter
  {
    ForexConverter _converter;
    Mockery _mocks;
    IExchangeRateRetriever _mockRetriever;
    
    [SetUp]
    public void Setup()
    {
      _mocks = new Mockery();
      _mockRetriever = _mocks.NewMock<IExchangeRateRetriever>();
      _converter = new ForexConverter(_mockRetriever);
    }
    [Test]
    public void TestForexCreation()
    {
      Assert.IsNotNull(_converter);
    }
    
    [Test]
    public void TestConvertUSDToPHP()
    {
      const double exchangeRate = 50.48;
      const double testAmount = 100.00;
// the following code uses NMock to assert that
// the _mockRetriever's GetRate method was called with the parameters
// "USD" and "PHP" and that it will return a value of exchangeRate
      Expect.Once.On(_mockRetriever).Method("GetRate").With("USD", "PHP").Will(Return.Value(exchangeRate));
      double inPesos = _converter.Convert("USD", "PHP", testAmount);
      Assert.AreEqual(exchangeRate * testAmount, inPesos, 0.001);
// the following line says that the test will fail if the above
// expectation (the call to _mockRetriever's GetRate() method, which
// is actually called by the _converter) is not met
      _mocks.VerifyAllExpectationsHaveBeenMet();
    }
    
    [Test]
    public void TestConvertSGDToPHP()
    {
      const double exchangeRate = 31.20;
      const double testAmount = 100.00;
// the following code uses NMock to assert that
// the _mockRetriever's GetRate method was called with the parameters
// "SGD" and "PHP" and that it will return a value of exchangeRate
      Expect.Once.On(_mockRetriever).Method("GetRate").With("SGD", "PHP").Will(Return.Value(exchangeRate));
      double inPesos = _converter.Convert("SGD", "PHP", testAmount);
      Assert.AreEqual(exchangeRate * testAmount, inPesos, 0.001);
// the following line says that the test will fail if the above
// expectation (the call to _mockRetriever's GetRate() method, which
// is actually called by the _converter) is not met
      _mocks.VerifyAllExpectationsHaveBeenMet();
    }
  }
}

now for IExchangeRateRetriever.cs:

1
2
3
4
5
6
7
namespace MockDemo1
{
  public interface IExchangeRateRetriever
  {
    double GetRate(string fromCurrency, string toCurrency);
  }
}

and finally ForexConverter.cs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
namespace MockDemo1
{
  public class ForexConverter
  {
    IExchangeRateRetriever _retriever;
    public ForexConverter(IExchangeRateRetriever retriever)
    {
      _retriever = retriever;
    }
    public double Convert(string fromCurrency, string toCurrency, double amount)
    {
      double result = 0.0;
      double exchangeRate = _retriever.GetRate(fromCurrency, toCurrency);
      result = amount*exchangeRate;
      return result;
    }
  }
}

Hopefully if you're someone who's interested in using mocks or NMock but you don't know where to start, you can use this sample as a starting point...

Posted 09-15-2006 4:27 PM by cruizer
Filed under: ,

Comments

smash wrote re: Sample NMock code
on 09-15-2006 3:50 AM

dre, i'll probably stick with simple tests muna. sumakit ang ulo ko sa syntax ng nmock  :)  

cruizer wrote re: Sample NMock code
on 09-15-2006 5:29 AM

nah, you'll get used to it :P

really it's very easy to use NMock to create mock implementations or stubs out of interface declarations.

jokiz wrote re: Sample NMock code
on 09-17-2006 7:06 PM

mastah, i think you should add a comment on the expectations para readable for starters (i.e. expecting this method will be called when this is executed, etc.)

cruizer wrote re: Sample NMock code
on 09-17-2006 7:09 PM

sige dude edit ko...thanks for the tip

jokiz's blog wrote Using NMock to test interaction of IComparable objects with it's IComparer
on 09-18-2006 8:30 PM

I have a requirement to sort a business object and obviously providing an IComparer for the said business

cruizer wrote TDD Step by Step, Part 1
on 11-24-2008 7:06 PM

In a hot discussion thread in msforums.ph about the value of unit testing and adopting test-driven development

cruizer wrote TDD Step by Step, Part 1
on 11-24-2008 7:07 PM

In a hot discussion thread in msforums.ph about the value of unit testing and adopting test-driven development

Copyright DevPinoy 2005-2008