In this post, i will be showing how you can mock property sets with your expected values or even action using JustMock. To begin, we have a sample interface:
Now, we can create a mock that will throw on any call other than the one expected, generally its a strict mock and we can do it like:
Here , the method for running though our expectation for set is Mock.ArrangeSet , where we can directly set our expectations or can even set matchers into it like:
In the example, any set for value not satisfying matcher expression will throw an MockException as this is a strict mock but what will be the case for loose mocks, where we also have to assert it. Here, let’s take an interface with an indexed property. Indexers are treated in the same way as properties, as with basic indexers let you access your class if it were an array.
We want to setup a value for a particular index, we then will pass that mock to some implementer where it will be actually called. Once done, we want to assert that if it has been invoked properly.
In the above example, both the values are user defined, it might happen that we want to make it more dynamic, In this example, i set it up for set with any value and finally checked if it is set with the one i am looking for.
This is more or less of mocking user sets , but we can further have it to throw exception or even do our own task for a particular set , like :
Or call the original setter , in this example it will throw an NotImplementedExpectation
Finally, try all these, find issues, post them to forum and make it work for you :-).
Hope that helps,
Subscribe to be the first to get our expert-written articles and tutorials for developers!