When
Applied, the When clause enables the arrangement only if certain conditions are satisfied. It specifies what must be true (along with any argument matcher, or IgnoreInstance() or IgnoreArguments() clauses used on the arrangement) for that arrangement to be executed and its expectations updated. To understand how to use the When method, check the next examples:
How It Works
Let's assume we have the following interface
:
Using JustMock, we can set a certain arrangement to be used only if another expectations are met. The below test method is a demonstration of this. The IsCalled
function is arranged to return true
only if Prop
is equal to "test".
How It Helps
There are situations where you need to make an arrangement that will only work when a certain condition is true. If the condition is related to that member's input arguments, you can use Matchers. However, if the condition is not related to them, you can achieve the desired behavior by using the When method.
Look at the following interface
:
In the test, we will check if GetResponse()
has been called once when Method
== "GET" and once more when Method
== "POST". Note that, the Method
property is unrelated to the GetResponse()
method.