Vladimir Dyuzhev
MockMotor Creator
Account Not Found Mock
Many flows (e.g. registration) require checking if a particular account number exists. It should not on the first call, but must after the registration. MockMotor supports this scenario.
If a mock account doesn't exist, the mock should return an error. If it does, the mock should return the account info.
How to support both of these outcomes?
Option 1. Conditional Response
The most straightforward and most manageable way is to have a single response that replies with either an error message or the account data depending if the mock account exists.
The response should:
1 Match the account with the data from the request.
2 However, select Ignore and Continue
in case the account is not found.
3 In the response payload, use a conditional branch. If $account
(or account
for JS) variable is empty, produce an error response.
If $account
(account
) is not empty, produce a successful data response.
4 Optionally, use the same $account
or account
variable in other fields, such as HTTP status.
Note the Ignore & Continue
selection on step 2. If you leave the selection in Fail Immediately
position, the response will fail hard as soon as it found no
matching account, and will not produce the custom error message.
Option 2. A Separate Response for Errors
Sometimes the response payload is already complicated enough to add the account not found condition there. In such a case we can extract the error generation into a separate response.
Our Not Found response will match when the account is not found, and our separate Found response will match when the account is found.
Not Found Response
To set up a dedicated Not Found response, we need to:
1 Match the account with the data from the request.
2 However, select Ignore and Continue
in case the account is not found.
3 Match the response with a script that is true only when account is not found - not($account)
or !account
.
4 In the response payload, form the error message. If we've got that far in this response, we know that the account is not found.
5 Similarly, hardcode error values for any other properties in the response where required.
Found Response
To set up a dedicated Found response, we need match by all the same conditions, but the account must be found:
1 Match the account with the data from the request.
2 Match the response with a script that is true only when account is found - exists($account)
or account!=null
.
3 In the response payload, form the successful message. We know that the account is found.
4 Similarly, hardcode success values for any other properties in the response where required.
How to Create an Account?
Great, but the registration process requires to create an account. How to do that?
To create an account in a response, use Create New
option for If no accounts found
checkboxes.
When this response is executed and the account is not found, the account is automatically created. The account is even pre-populated with the values you used to select it with:
Here the account is created and its officeId
property is populated with $input//*:office-id
value.
The rest of its properties you can populate in the Update Mock Accounts
section.