testing

Testing tools, testing philosophy & methodology...
lwhorton 2016-04-26T15:22:13.000006Z

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?

nberger 2016-04-26T15:53:48.000007Z

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

lwhorton 2016-04-26T15:54:34.000008Z

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?

lwhorton 2016-04-26T15:55:23.000009Z

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

lwhorton 2016-04-26T15:55:56.000010Z

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

nberger 2016-04-26T15:57:54.000011Z

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

lwhorton 2016-04-26T16:00:47.000012Z

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

lwhorton 2016-04-26T16:01:02.000013Z

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

lwhorton 2016-04-26T18:56:50.000014Z

wohoo, finally figured it out @nberger, thanks:

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

nberger 2016-04-26T19:04:29.000015Z

@lwhorton: sweet!!

2016-04-26T22:26:47.000016Z

hey @lwhorton is the intermediate io/file necessary?

lwhorton 2016-04-26T22:27:12.000017Z

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

lwhorton 2016-04-26T22:27:42.000018Z

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

lwhorton 2016-04-26T22:27:51.000019Z

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

lwhorton 2016-04-26T22:28:13.000020Z

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

2016-04-26T22:28:31.000021Z

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

2016-04-26T22:28:55.000022Z

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

lwhorton 2016-04-26T22:30:04.000023Z

i’ll take a look, thanks