marick / Midje

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

Check arity of functions being mocked #433 broke Mocking of Amazonica APIs #452

Closed carnellj closed 5 years ago

carnellj commented 5 years ago

Hi @philomates ,

I am seeking some advice. The Amazonica libraries allow you to pass in a map containing the credentials for the call. For example:

(ec2/describe-images {:endpoint properties/ec2-endpoint} :image-ids [ami-id]))

This first parameter on every call is option and they are basically using a macro call and coercion to map everything to the underlying Amazon call. As a result, almost all of the Amazonica APIs have only only 1 parameter and their code coerces the extra leading parameter.

My mocks always include the parameters {:endpoint} map and then extra value. However, with fix #433 you are now forcing the mocked parameters to match the expected parameter list. I am not disagreeing that this is necessarily a bad thing, but I am seeking your thoughts on how I can work through the issue I described above. I am being forced to upgrade for (JDK 11 dependencies) and this is now breaking my builds.

Any help or thoughts you can give would be appreciated.

Thanks,
  John 
philomates commented 5 years ago

Hey @carnellj,

I imagine you are referring to the following, using [amazonica "0.3.139"] and [midje "1.9.5"]

(ns reproduce-amazonica-issue
  (:require [midje.sweet :refer :all]
            [amazonica.aws.ec2 :as ec2]))

(defn call-amazonica []
  (ec2/describe-images :owners ["self"] :image-ids ["ami-f00f9699" "ami-e0d30c89"]))

(fact
  (call-amazonica) => irrelevant
  (provided
    (ec2/describe-images :owners ["self"] :image-ids ["ami-f00f9699" "ami-e0d30c89"]) => nil))

which results in

FAIL at (core_test.clj:13)
You faked #'describe-images with an argument count that doesn't match the function's defined arg
uements:
Provided 4 argument(s), where valid options are: 0, 1 arguments

FAIL at (core_test.clj:13)
These calls were not made the right number of times:
    (ec2/describe-images :owners ["self"] :image-ids ["ami-f00f9699" "ami-e0d30c89"]) [expected
at least once, actually never called]

I believe this is due to an issue with how the amazonica library defines the :arglists metadata for its wrapped amazon functions (here). It builds the arglist to match the java api, which doesn't match how the actual clojure wrapper should be called.

For example:

(-> #'ec2/describe-images meta :arglists)
;; => ([] [describe-images-request])

I think this leaves us with 3 options:

I think the 1st is the most sensical, but maybe not the easiest, with the last being a last resort hack. I can add the config parameter if you'd like, but I'd prefer to see if amazonica can be adapted first.

cheers!

carnellj commented 5 years ago

Hi Phillip,

Thanks for the quick turnaround on my question. I know appreciate you taking the time. What you have pointed out is exactly the problem. I will reach out to the Amazonica team and see if they are willing or able to address it.

Thanks again for the help.

- John