How to do realworld generating test with spec?

spec is easy to use for validation, because in this case specs are easy to write. basically you don’t really care about the value, just care about the type or data shape. But for generating test, I found, at least for me, the spec generators are really hard to write, the most simple way I could found is write a few sample data with set, like #{"A" "B" "C"}. However this seems to be useless for test.
for example, code like:

(defn get-ident [idt-key entity] [idt-key (get entity idt-key)])

;; usage
(get-ident :x/id {:x/id 1 :x/y 2}) ;; => [:x/id 1]

The idt-key could be a qualified-keyword?, what would be the spec for entity, a map contains a key called idt-key or just map?.
Any experiences or tips will be much appreciated.

You may find this video helpful:

The video is one of a series which I also recommend you take a look at.

Hope that helps!

Joe

1 Like

Also should include this post on shrinking your custom generators.

Joe.

1 Like

@MageMasher thanks for sharing!

I knew I can write generators, but the problem is not about the api or syntax, is about how to write suitable generators.

So you have 2 inputs that are related, you want the entity to include the key, right? This is what I did in a similar case: https://blog.jakubholy.net/generative-testing-lessons-learned/#_spec_a_wrapper_function_that_merges_several_inputs_so_that_you_can_ensure_relationships_between_them

1 Like

Great article! I think I get the idea. That is I can generate the entity depends on the value of the key.