Mock Objects Advanced Features

In the last post, I talked about how to use Mock Objects in Unit Tests. In this post, I am going to talk about some advanced features of the Mock Objects.

It is worth going back and reading my previous posts on Mock Objects if you have not already done so.

For reference, below is a video introducing the basics of Mock Objects.

Building Call Logs

Example of how to build a call log
Most of the interesting stuff happens here.

The Generate Call method includes 2 built-in advanced features. These come for free with the toolkit and simply require wiring up some extra parameters.

Numeric Tolerance

Applying one tolerance to all numeric parameters.

If you wire up the numeric tolerance, any parameters in the parameters cluster that are numeric (single, double, extended) will have the added tolerance attribute. This is used when those parameters are verified. Note as of right now the algorithm is not recursive so it won’t detect numerics nested in clusters or arrays. Perhaps that may get changed in future versions. It is an open source project, so feel free to add it if you would like. In the meantime, you can create custom parameters.

Applying different numeric tolerances to different parameters.

By default, all numerics will get the same tolerance. If you have multiple numeric inputs and want a different tolerance for each, you can do that. You just have to build the parameters array separately and pass that into the Generate Call method.

Ignoring Parameters

Another built-in feature is the ability to ignore an input. There may be various reasons to do this. Typically it has to do with not being able to predict the value. Perhaps useful for references?

Extensions

Here is the lvlib.
A UML diagram showing the relationship of all the classes.

If you delve into the SAS_LVMock lvlib, you will find several classes. Several of these classes can be inherited from to add additional functionality. This is useful if you want to do custom verification. If you do create custom Call Logs or Parameters, I do recommend you write some unit tests for them. You can use the existing tests as examples. You can find them here: https://gitlab.com/sas_public/mock-object-helper

NOTE all of the verification s performed by the Desired Call Log. You can add custom verification without editing the Mock Object, the Mock Object Helper or how calls are logged.

Custom Call Logs

Note the dynamic Dispatch Terminal is the Desired Call Log.
The default Verify implementation for Call Log.

By Default the Call Log requires that Actual Call Log occurs in the same order as the Desired Call Log as seen in the block diagram above. There are some scenarios where you may want to override the Call Log. Here are some examples.

  • If we don’t care about the order of the calls
  • If we want to allow an arbitrary number of calls to a particular method.
  • If we want to specify that certain calls must happen, but don’t care if other additional calls are made

Custom Parameters

Note the dynamic dispatch terminal is the Desired.
Default Parameter Verify Value simply checks for equality.
Don’t Care Parameters always pass regardless of value.
Numeric with tolerance checks that the value is within tolerance.

By Default the Parameter Verify Value Method simply does equality. As you can see in the above Block Diagrams, the Don’t Care Parameters always pass regardless of the value, while Numeric With Tolerance, checks the tolerance.

Here are some potential examples where you would want a custom parameter

  • Case insensitive string matches
  • REGEX string matches
  • Arbitrary ranges for numerics including GTE, LTE, etc
  • Custom matching for objects or clusters
  • Checking for valid references

Leave a Reply

Your email address will not be published. Required fields are marked *

*