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