When testing functions that reference some state (atom, ref, or agent), it's nice to be able to quickly replace the value of the state in the context of the test. When your function only interacts with one piece of state, a simple call to with-redefs will do the trick. However, there are times when the function that you're calling updates many different pieces of state, and you'd like to be able to redef all of them with one call. The expectations testing framework (v 1.4.16 and above) provides you the ability to redef all atoms, refs, and agents in a namespace with one call to redef-state.
(this same feature existed in expectation.scenarios as 'localize-state')
Let's take a look at the following contrived namespace
In the above namespace we have two atoms that are both updated when you process an update. Testing that the atoms are updated is fairly simple, which the tests below demonstrate.
Unfortunately, these tests will not both pass, as they both update the same atom. We could clean up at the end of each test, but it's usually cleaner to simply redef the atoms in the context of the test. The tests below use with-redefs to ensure that the state is only manipulated in the context of the tests.
At this point the tests all pass. This solution works fine, but expectations gives you the ability to trim a bit of code and simply specify the namespace instead. The following tests specify the namespace and let expectations take care of the rest.
That's it. Now all atoms, refs, and agents that are defined in the 'blog' namespace will be redefined within the context of the (redef-state) call. It's also important to note that redef-state can take as many namespaces as you'd like to specify in the first arg vector.
Is it possible to assign a specific state to your atoms etc., or does redef-state only make them empty?
ReplyDelete@Ben,
ReplyDeleteredef state actually uses whatever value is already in there - which can lead to issues if you aren't careful. For example, if you alter some state in test 1 and then use redef state in test 2, you'll not have an empty data structure, you'll have whatever test 1 left in there.
If you want a non-empty value, I'd use either with-redefs alone or in combination with redef-state.
Thanks for using expectations.
Cheers, Jay