hey guys, i’m wondering how to go about using mock testing files in clojure? what’s the idiomatic way, for example, to read-in a file that’s sitting in spec/path/to/mock.file.json
and test against it, for example?
@lwhorton: yes, I'd use slurp to read the file and data.json or cheshire to parse it, and that's it
but how about the whole relative/absolute file issue? if my spec/some-mod/mock.file.json
is needed for the test, how do I read that so it works across multiple machines/environments?
this is what I’m currently using, but I just don’t know if i’m anywhere close:
(let [cwd (.getAbsolutePath (io/file ""))
res (#’sut/fn (str cwd "/spec/clj/some-module/mock-thing.json"))
for some reason I can’t get at the module I want to test (it throws fn is private
) unless I use #’module/fn
Oh. You can put them in a directory that it's in resources paths, then you can get to it with io/resource and the relative path from that resources directory
is it bad form to setup :resource-paths [“test/clj”]
?
I’m really fuzzy on the details of source-paths and resource-paths, I should probably find a blog to read...
wohoo, finally figured it out @nberger, thanks:
(slurp (io/file (io/resource “relative/path/in/resource-paths”)))
@lwhorton: sweet!!
hey @lwhorton is the intermediate io/file
necessary?
I honestly can’t remember, i was essentially mashing buttons until it worked.
i know resource looks up in the resource-path the relative file, and returns the URL
so I assume you need to actually make a file object out of the URL?
maybe slurp can take just the URL… actually it seems like it can.
if you have the time/energy, try without io/file
which should work as well
also, if what you are serializing is clojure data, you could use edn files and use spit
i’ll take a look, thanks