marick / Midje

Midje provides a migration path from clojure.test to a more flexible, readable, abstract, and gracious style of testing
MIT License
1.69k stars 129 forks source link

Folded prereqs fail in against-background #54

Closed sritchie closed 11 years ago

sritchie commented 13 years ago

I came across an issue with the use against-background in folded prerequisites. Let's say I'm testing these functions:

(defn square [x] (* x x))

(defn black-hole
  "Throws an exception, ALWAYS!"
  [& _]
  (throw (IllegalArgumentException. "Dead!")))

(defn square->string [x]
  (str (square (black-hole x))))

And want to mock out (square (black-hole x)). provided does what I want, and never actually passes anything into the black hole:

(fact "This pass just fine."
  (square->string ..num..) => "9"
  (provided (square (black-hole ..num..)) => 9))

while against-background actually passes in the arguments:

(fact "This one dies!"
  (square->string ..num..) => "9"
  (against-background (square (black-hole ..num..)) => 9))

This examples a little crazy, but any inner function that can't accept a string (or whatever else I use for the mocked variable) will fail in the against-background case.

Thanks, Midje is awesome! Sam

marick commented 13 years ago

Darn. It's a bug.

"Folded prerequisites" https://github.com/marick/Midje/wiki/Folded-prerequisites are only unfolded when they appear in (provided...), not in background.

One workaround, if you need it, is to unfold the prerequisite yourself:

 (against-background 
     (black-hole ..num..) => ..black-hole-value..
     (square ..black-hole-value..) => 9

I'll file an issue.

On Oct 2, 2011, at 3:20 PM, Sam Ritchie wrote:

I came across an issue with the use against-background in folded prerequisites. Let's say I'm testing these functions:

(defn square [x](* x x))

(defn black-hole "Throws an exception, ALWAYS!" [& _](throw %28IllegalArgumentException.)))

(defn square->string [x](str %28square %28black-hole x%29%29))

And want to mock out (square (black-hole x)). provided does what I want, and never actually passes anything into the black hole:

(fact "This pass just fine." (square->string ..num..) => "9" (provided (square (black-hole ..num..)) => 9))

while against-background actually passes in the arguments:

(fact "This one dies!" (square->string ..num..) => "9" (against-background (square (black-hole ..num..)) => 9))

This examples a little crazy, but any inner function that can't accept a string (or whatever else I use for the mocked variable) will fail in the against-background case.

Thanks, Midje is awesome! Sam

Reply to this email directly or view it on GitHub: https://github.com/marick/Midje/issues/54


Brian Marick, Artisanal Labrador Now working at http://path11.com Contract programming in Ruby and Clojure Occasional consulting on Agile

marick commented 13 years ago

The specific problem is that fakes/unfolding-step is only called in the expansion of semi-sweet/expect form. Something similar will have to be done somewhere around prerequisites/expand-prerequisites-into-fake-calls

sritchie commented 13 years ago

Great, and thanks for the workaround, that looks good.

AlexBaranosky commented 12 years ago

Poking at this code, and it is poking me back, unfortunately. It really seems to me like unfolded fakes should work in background forms. the inconsistency makes me really want to fix this bug.

I factored this function out of unfold-expect-form__then__stay_put in fakes.clj, thinking I could use it in extract-state-descriptions+fakes in background.clj to unfold all of the fakes:

(defn unfold-all-pending-fakes [pending]
  (loop [[finished pending substitutions] [[] pending {}]]
    (if (empty? pending)
      finished
      (recur (unfolding-step finished pending substitutions)))))

Tried to use it like:

is-start-of-checking-arrow-sequence?
(let [arrow-seq (take-arrow-sequence in-progress)]
  (recur (conj expanded (mapcat tag-as-background-fake (unfold-all-pending-fakes [arrow-seq])))
    (drop (count arrow-seq) in-progress)))

That was a massive fail as I don't know if I'm inches from getting this working or miles away.

Any comments on this approach, Brian? Is it helpful to you? Is it way off?

AlexBaranosky commented 12 years ago

We're going to hold off on fixing this until after we rework the approach we use for background/against-background.

marick commented 11 years ago

I'm closing this and consolidating all the "background" issues into a single issue that can serve to help us avoid mistakes next go-round.