-
Notifications
You must be signed in to change notification settings - Fork 10
Mocking
The mock feature allows you to change or create any global variable for the duration of a test. After the test has finished, the previous value will be restored.
It is generally not recommended that you use the mocking facility on any of the variables that drive the test framework (anything in the .tst
namespace).
The mock
keyword behaves identically to the standard set
keyword, with the exception that it only operates on in-memory variables and will not perform disk access. The qspec framework keeps track of every time the mock
keyword is used and ensures that variables are returned to their previous pre-mock
value. It is safe to use several sequential mock
commands; the original value will be restored after the test has finished regardless.
If the variable being mocked existed beforehand, then it’s value will simply be changed. If it did not exist, then it will be created. After the test has run, variables that existed beforehand are returned to their previous value, variables that did not exist beforehand are deleted. The only exception is top level namespaces. Top level namespaces themselves, such as .h
or .q
cannot be mocked directly, though values within them can be. Furthermore, while a top level namespace can be created with the use of mock
by mocking a new variable in a new namespace that did not previously exist, after the test has finished, the new namespace will still exist, though no variables will be present within it.
Let’s illustrate that with an example:
q)key `
`q`Q`o`h`utl`tst
q)`.foo.bar mock 3
`.foo.bar
q)key `
`q`Q`o`h`utl`tst`foo
q).foo
| ::
bar| 3
...
/ Tests have finished
...
q).foo.bar
'.foo.bar
q)key `
`q`Q`o`h`utl`tst`foo
q).foo
| ::
Please note that for this example and others on this page, we are ignoring that you would need to have written a test to actually be able to use the functions presented. To see a more concrete example of this feature, please see the Tutorial
.tst.restore
is the function that actually returns the program’s state to where it was before mock
was used. .tst.restore
is niladic. While the use of this function explicitly is not encouraged as the test framework tends to do a very good job of handling concerns regarding mocked variables, at some point you may require greater control over the values that are mocked