In this post , i show how you can benefit from sequential mocking feature[In JustMock] for setting up expectations with successive calls of same type. To start let’s first consider the following dummy database and entity class.
Now, our test goal is to return different entity for successive calls on IDataBase.Get<T>(). By default, the behavior in JustMock is override , which is similar to other popular mocking tools. By override it means that the tool will consider always the latest user setup.
Therefore, the first example will return the latest entity every-time and will fail in line #10:
We can solve it the following way using a Queue and that removes the item from bottom on each call:
This will ensure that right entity is returned but this is not an elegant solution. So, in JustMock we introduced a new option that lets you set up your expectations sequentially. Like:
The “InSequence” modifier will tell the mocking tool to return the expected result as in the order it is specified by user. The solution though pretty simple but neat and way too simpler than using a collection to solve this type of cases.
Hope that helps
P.S. The example shown in my blog is using interface don’t require a profiler and you can even use a notepad and build it referencing Telerik.JustMock.dll, run it with GUI tools and it will work. But this feature also applies to concrete methods that includes JM profiler and can be implemented for more complex scenarios.