Doing your first mock with JustMock

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!!

6 Comments

  • I am interested to know the differences between JustMock and Moq. What cool new features JustMock brings on the table?

    Is there something handy to mock and test multithreaded stuffs?

  • About multithreading, I was wondering if there's any easier way to mock and unit test Dispatcher, or SynchronizationContext. You can see from this article what pain I went through to make Dispatcher unit testable. The last of the article are all unit tests:

    http://www.codeproject.com/KB/WPF/parallelwork.aspx

    It would be great if JustMock could support BDD as well. Especially the SubSpec format, which I absolutely love.

  • @Omar : You are wrongly intermixing the two things IMHO. It is true that Mocking is an integral part of writing of Unit Test, but supporting BDD or Regular Style is up to the Unit Testing Framework, For example MSpec support BDD form the ground up but the other does not, you can use any mocking framework with MSpec.

    Regarding Mocking the Framework types, I know the JustMock supports to Mock eveything, but there is another way to write unit test without those Hard to Mock Types. Use the Proxy pattern, Check the System.Web.Abstraction.

  • Been playing with JustMock. Love the syntax, and promises to be really cool. However, a lot of the examples provided with it don't work and crash, so I'm not sure what to think. It is a beta, so I'm going to chalk it up to that for now, but if a feature isn't working, why give us examples that use it?

  • @JHurdlow

    Its nice that you liked the syntax. About the test fails, there are some issues with VS host and profiler that causing the test to fail in different circumtances, it only happens while running in the dubug mode inside the IDE, not happens with MSBuild, we are working on this and it will be hopefully fixed in coming build.

  • Interesting that it would work in MSBuild but fail in VS, very strange. But it sounds like you have it under control. Looking forward to the next build. :)

Comments have been disabled for this content.