Skip to content

Instantly share code, notes, and snippets.

@kurtharriger
Created April 16, 2012 01:06
Show Gist options
  • Select an option

  • Save kurtharriger/2395697 to your computer and use it in GitHub Desktop.

Select an option

Save kurtharriger/2395697 to your computer and use it in GitHub Desktop.
midje provides does not work in let bindings
(use 'midje.sweet)
(defn mocked [x] "ERROR: THIS SHOULDN'T HAVE BEEN CALLED")
(defn handler [r]
(let [body (mocked (get-in r [:params :id]))]
{:status (if (re-find #"ERROR" body) 500 200)
:body body
:content-type "text/plain"
}
))
;;. FAIL at (NO_SOURCE_FILE:1)
;;. Expected: 200
;;. Actual: 500
;;.
;;. FAIL at (NO_SOURCE_FILE:1)
;;. You claimed the following was needed, but it was never used:
;;. (mocked 3)
;;.
;;. FAIL at (NO_SOURCE_FILE:1)
;;. Expected: "OK"
;;. Actual: "ERROR: THIS SHOULDN'T HAVE BEEN CALLED"
(fact "Fails: mocks do not work in let bindings"
(let [request {:params {:id 3}}
response (handler 1)]
(:status response) => 200
(:body response) => "OK")
(provided
(mocked 3) => "OK" :times 1
))
(comment
(do
(midje.util.report/fact-begins)
(midje.internal-ideas.wrapping/midje-wrapped
(midje.internal-ideas.fakes/with-installed-fakes
(midje.ideas.background/background-fakes)
(do
fact
"Fails: mocks do not work in let bindings"
(let
[request {:params {:id 3}} response (handler 1)]
(midje.internal-ideas.wrapping/midje-wrapped
(midje.semi-sweet/expect
(:status response)
=>
200
:position
(midje.internal-ideas.file-position/line-number-known 1)))
(midje.internal-ideas.wrapping/midje-wrapped
(midje.semi-sweet/expect
(:body response)
=>
"OK"
:position
(midje.internal-ideas.file-position/line-number-known 1)
(midje.semi-sweet/fake
(mocked 3)
=>
"OK"
:position
(midje.internal-ideas.file-position/line-number-known 1)
:times
1)))))))
(midje.util.report/fact-checks-out?))
)
;;. The journey is the reward. -- traditional
(fact "This works, but IMHO is more difficult to read when validating multiple properties"
(let [request {:params {:id 3}}]
(handler request)) => (contains {:status 200 :body "OK" :content-type "text/plain"})
(provided
(mocked 3) => "OK" :times 1
))
;;. Work joyfully and peacefully, knowing that right thoughts and right
;;. efforts will inevitably bring about right results. -- Allen
(fact "Can also use predicates, but not as obvious as => syntax"
(let [request {:params {:id 3}}]
(handler request) => (contains {:status pos? :body (complement nil?)}))
(provided
(mocked 3) => "OK" :times 1
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment