Fork me on GitHub
#fulcro
<
2019-12-08
>
sheluchin12:12:05

Just getting started with testing. Could someone please give me some pointers on this simple test:

(defn deal-card-to-player*
  "Take a card from the deck and add it to the player's hand"
  [state-map deck-id player-id card-ident]
  (-> state-map
    (give-card-to-player* ,,, player-id card-ident)
    (take-from-deck* ,,, deck-id card-ident)))

(specification "Mutation Helpers"
  (component "High level - Abstract actions like dealing, which act on multiple things"
    (behavior "Deals a card from the deck to the player"
      (when-mocking
        (give-card-to-player* state-map player-id card-ident)
        =1x=> (do
                (assertions
                  state-map => {:x 41}
                  player-id => 1
                  card-ident => [:card/id "h2"])
                {:x 42})
        (take-from-deck* state-map deck-id card-ident)
        =1x=> (do
                (assertions
                  state-map => {:x 42}
                  deck-id => 1
                  card-ident => [:card/id "h2"])
                {:x 43})
        (assertions
          "Calls each mutation helper once"
          (deal-card-to-player*
            ; mocked state-map, values don't matter, just asserting calls and
            ; transformations in the stabby macro
            {:x 41}
            1
            1
            [:card/id "h2"]) => {:x 43})))))

tony.kay16:12:48

If you’re going to do mocking, you should use guardrails and spec your functions so that the mocking system can verify your “hooking things up” correctly. See when-mocking! , Fulcro guardrails, and my testing video on youtube (Clj West talk). In other words: Your comment that “stat map values don’t matter” isn’t completely true. The value does not matter, but the shape and types of data do. Then there’s commentary on how to spec these, since you’re passing a big map of state. I’d just mention what each function requires (i.e. take-from-deck just needs to see a deck in there).

sheluchin10:12:39

Thank you. I've started integrating guardrails into my code and workflow. I do wonder how I might get the results in vim as I'm coding, but that's a question for another place. The tip about specifying what must be present in the big state map is helpful too, and seems obvious in retrospect.