In this post, i will start with a  more traditional mocking example that  includes a fund transfer scenario between two different currency account using JustMock.Our target interface that we will be mocking looks similar to:

  1. public interface ICurrencyService
  2. {
  3.     float GetConversionRate(string fromCurrency, string toCurrency);
  4. }

 

Moving forward the SUT or class that will be consuming the  service and will be invoked by user [provided that the ICurrencyService will be passed in a DI style] looks like:

  1. public class AccountService : IAccountService
  2. {
  3.     private readonly ICurrencyService currencyService;
  4.  
  5.     public AccountService(ICurrencyService currencyService)
  6.     {
  7.         this.currencyService = currencyService;
  8.     }
  9.  
  10.     #region IAccountService Members
  11.  
  12.     public void TransferFunds(Account from, Account to, float amount)
  13.     {
  14.         from.Withdraw(amount);
  15.         float conversionRate = currencyService.GetConversionRate(from.Currency, to.Currency);
  16.         float convertedAmount = amount * conversionRate;
  17.         to.Deposit(convertedAmount);
  18.     }
  19.  
  20.     #endregion
  21. }

As, we can see there is a TransferFunds action implemented from IAccountService  takes in a source account from where it withdraws some money and a target account to where the transfer takes place using the provided conversion rate.

Our first step is to create the mock. The syntax for creating your instance mocks is pretty much same and  is valid for all interfaces, non-sealed/sealed concrete instance classes. You can pass in additional stuffs like whether its an strict mock or not, by default all the mocks in JustMock are loose, you can use it as default valued objects or stubs as well.

  1. ICurrencyService currencyService = Mock.Create<ICurrencyService>();

Using JustMock, setting up your expectations and asserting them always goes with Mock.Arrang|Assert and this is pretty much same syntax no matter what type of mocking you are doing. Therefore,  in the above scenario we want to make sure that the conversion rate always returns 2.20F when converting from GBP to CAD. To do so we need to arrange in the following way:

  1. Mock.Arrange(() => currencyService.GetConversionRate("GBP", "CAD")).Returns(2.20f).MustBeCalled();

 

Here, I have additionally marked the mock call as must. That means it should be invoked anywhere in the code before we do Mock.Assert, we can also assert mocks directly though lamda expressions  but the more general Mock.Assert(mocked) will assert only the setups that are marked as "MustBeCalled()”.

Now, coming back to the main topic , as we setup the mock, now its time to act on it. Therefore, first we create our account service class and create our from and to accounts respectively.

  1. var accountService = new AccountService(currencyService);
  2.  
  3. var canadianAccount = new Account(0, "CAD");
  4. var britishAccount = new Account(0, "GBP");

Next, we add some money to the GBP  account:

  1. britishAccount.Deposit(100);

Finally, we do our transfer by the following:

  1. accountService.TransferFunds(britishAccount, canadianAccount, 100);

Once, everything is completed, we need to make sure that things were as it is we have expected, so its time for assertions.Here, we first do the general assertions:

  1. Assert.Equal(0, britishAccount.Balance);
  2. Assert.Equal(220, canadianAccount.Balance);

Following, we do our mock assertion,  as have marked the call as “MustBeCalled” it will make sure that our mock is actually invoked. Moreover, we can add filters like how many times our expected mock call has occurred that will be covered in coming posts.

  1. Mock.Assert(currencyService);

So far, that actually concludes our  first  mock with JustMock and do stay tuned for more.

Enjoy!!


Comments

Comments are disabled in preview mode.