Lesson Learned: Don't Use Low-Level Lib To Test High-Level Code
source link: https://blog.jakubholy.net/2013/05/21/lesson-learned-dont-use-low-level-lib-to-test-high-level-code/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Lesson Learned: Don't Use Low-Level Lib To Test High-Level Code
The case: I want to test that the method login sends the right parameters and transforms the result as expected. Login invokes post-raw which calls an HTTP method. Originally I have tried to test it by using the library clj-http-fake but it proved to be unnecessarily complex. It would be much better to fake post-raw itself for testing login and test the original post-raw and its HTTP interaction separately, using that library.
There are two problems with using the low-level clj-http-fake library for testing the login function:
- Both the calls I need to make need to be wrapped in its with-fake-routes, which makes the test more complex.
- More seriously, I need to drop down from the level of Clojure data structures to JSON encoded as a String. The transformations between the two would pollute the test and be meaningless from the point of view of what I want to test.
Pseudocode:
// "Business" logic
function login(..)
authorizationToken = post-raw(...)
return (new function derived from post-raw with some parameters such as auth.token bound)
// Test
...
fake post-raw(request) to return "my-authorization-token"
var post-authenticated = login(..)
assert post-authenticated is function
fake post-raw(request) to return request
assert post-authenticated(dummy-values) = {dummy-values + the auth. params}
// Testing post-raw isn't important now
See the clj-zabbix-api's core_test.clj for the full Clojure test and core.clj for the code. Disclaimer: It is not very good Clojure code. I am just learning :-)
Are you benefitting from my writing? Consider buying me a coffee or supporting my work via GitHub Sponsors. Thank you! You can also book me for a mentoring / pair-programming session via Codementor or (cheaper) email.
Allow me to write to you!
Let's get in touch! I will occasionally send you a short email with a few links to interesting stuff I found and with summaries of my new blog posts. Max 1-2 emails per month. I read and answer to all replies.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK