2

This question is not about specific libraries (although some of them will be used in end), but more about how to structure your application code to make side-effects functions unit testing possible. If we should do it at all?

Obviously, it is clear and simple to do testing of pure side-effects free functions, you pass the input, and you assert the output.

There are 2 (very roughly) types of testing, unit and integration. Lets focus on unit testing here.

So, if you have a function which reads from file or writes into files (using slurp/spit, for example), or work with database, or ring application, or core.async channels, how to do unit testing of such things in Clojure? Are mocks involved, if yes, how they should be defined? Are there binding, with-{redefs, redefs-fn, local-vars} involved? Should I define defprotocol in order to reify (aka mock?) implementation during unit testing?

One of the value of testing (the main value?) is to force a better design on the application code, so maybe the testing in Clojure is special in that sense, that you are forced to structure the application code specific way to make side-effects functions unit testing in Clojure possible?

Or I completely miss the point?

P.S.1: I am comfortable working and developing with Clojure small/mid-size applications (for sure still a way to go), although testing part is still not clear to me completely.

P.S.2: Another big topic is integration testing in Clojure, but it will deserve a separate SO question.

1
  • Introducing midje Commented Nov 18, 2015 at 11:50

2 Answers 2

1

In general you test code with side effects be implementing setup and teardown artifacts for your tests. These create the base/initial state and help evaluate the results.

Sign up to request clarification or add additional context in comments.

1 Comment

An example would be a great addition. Do you know anything specific on GitHub to take a look?
1

I'd strongly encourage you to avoid with-redefs and friends if at all possible. We use protocols for most all of our interactions with external services and dependency inject everywhere, so testing is a breeze. Of course, we still try and make functions as pure as possible and carefully manage effects, but generally we've been happy with protocols when needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.