Clojurians
#testing
<
2016-04-26
>

This page is not created by, affiliated with, or supported by Slack Technologies, Inc.

lwhorton15:04:13

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?

nberger15:04:48

@lwhorton: yes, I'd use slurp to read the file and data.json or cheshire to parse it, and that's it

lwhorton15:04:34

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?

lwhorton15:04:23

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"))

lwhorton15:04:56

for some reason I can’t get at the module I want to test (it throws fn is private) unless I use #’module/fn

nberger15:04:54

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

lwhorton16:04:47

is it bad form to setup :resource-paths [“test/clj”]?

lwhorton16:04:02

I’m really fuzzy on the details of source-paths and resource-paths, I should probably find a blog to read...

lwhorton18:04:50

wohoo, finally figured it out @nberger, thanks:

(slurp (io/file (io/resource “relative/path/in/resource-paths”)))

nberger19:04:29

@lwhorton: sweet!!

bensu22:04:47

hey @lwhorton is the intermediate io/file necessary?

lwhorton22:04:12

I honestly can’t remember, i was essentially mashing buttons until it worked.

lwhorton22:04:42

i know resource looks up in the resource-path the relative file, and returns the URL

lwhorton22:04:51

so I assume you need to actually make a file object out of the URL?

lwhorton22:04:13

maybe slurp can take just the URL… actually it seems like it can.

bensu22:04:31

if you have the time/energy, try without io/file which should work as well

bensu22:04:55

also, if what you are serializing is clojure data, you could use edn files and use spit

lwhorton22:04:04

i’ll take a look, thanks