Fork me on GitHub

how to test side-effected functions with clojure.spec?

Oliver George08:02:33

That complicates things but there are some features you might like to check out. Pretty sure you can stub out functions. That might let you isolate you're logic for testing.

Oliver George08:02:45

I haven't used that feature myself.


@dbushenko Attach the spec as a validator to the var/ref/agent/atom, maybe...!


@dbushenko @olivergeorge regarding stubbing a function — after fdefing the function, use

(stest/instrument `your-func {:stub #{`your-func}})
to use the :ret in the fdef instead of calling the real function, fyi


every doesn’t use res on its element predicate argument:

=> (s/form (s/every string?))
 #object[user$eval8666$fn__8668 0x5ba5f0bd “user$eval8666$fn__8668@5ba5f0bd"])

I’ve found which covers that but with a larger scope. @alexmiller Would it be sensible to open a ticket just for the resolution of the predicate?

Alex Miller (Clojure team)13:02:13

No, please add to 2035 if it's not already covered by the patch there

Alex Miller (Clojure team)13:02:02

I think there may actually be another ticket for this already though

Alex Miller (Clojure team)13:02:07

Nah, I was thinking of 2059 which is in explain-data


are js objects spec-able without converting to cljs data structures?


you can always write arbitrary predicates that check whatever you want


Hi, I have a list of maps and each map has an :idx key which is an int. Now what I want is that given one list every :idx is a unique int. Is there a way to enforce that?


(= (count my-list) (-> (map :idx my-list) distinct count))


Cool, even exercise respects that function, thank you @frank

Alex Miller (Clojure team)16:02:25

@sveri the collection spec have a :distinct option

Alex Miller (Clojure team)16:02:52

Although I guess that goes farther than you want


Hi here! Let’s say I’ve got a map of user. The key ::password must be present if and only if a user is new (the key ::new? is bound to a truthy value). Is it possible to express this with clojure.spec?


@alexmiller I have seen that, but I suspect it means distinct for the whole map and not only for the :idx key

Alex Miller (Clojure team)17:02:32

@piotr2b: sure, use a custom predicate

Oliver George19:02:21

@mss if you can convert to clojure data structures first then you'll get access to more spec features. In particular s/keys & all the standard generators


@sveri test.check has distinct-by generators for that sort of thing; I don't know if spec exposes it in the collection specs or not