简体   繁体   中英

clojure testing using midge

My function accepts a map argument in which i get the deposit value from it. Currently I'm able to test scenarios like whether the deposit value is empty or whether the deposit value is greater than a certain value etc. My API tracks the number of times a user makes the deposit and keeps track of this using an atom and when user has made more than 4 deposits, he/she should get a certain error message. How can I test this scenario using midje?

Below is my sample test case:

(facts "Mytesting"
       (fact "Invalid Amount"
             (let [response (httpserver/app (mock/request
                                              :post "/deposit"
                                              {:deposit "9"}))] ;
               (:status response) => 422
               (:headers response) => {"Content-Type" "application/json; charset=utf-8", "X-Content-Type-Options" "nosniff", "X-Frame-Options" "SAMEORIGIN", "X-XSS-Protection" "1; mode=block"}
               (:body response) => (json/write-str {:status-code -2 :status-msg "Invalid Amount"}))))

Well, for starters, you should write your tests first :-).

But since the code already exists, I'll tell you how I usually work:

  • I split up the tests between testing the HTTP routing (like what you're doing here). Here I use the provided functionality provided by Midje to verify that the backing functions are invoked correctly.
  • Other tests invoke the backing functions directly, passing in a map that resembles a Ring request (or Yada context, or whatever you use). Here you can also use provided to mock out any other functionality that's not immediately relevant.
  • The backing functions usually delegate to the actual "business" functions that don't know about http requests, but that just accept and return the business entities (usually just maps and vectors in Clojure).

You can of course also add end-to-end tests that verify the behavior of the entire code stack, but that may involve setting up a database, etc...

To know more about how to test your specific code, I would need to know more about the inner workings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM