Midje, provided not working as I expect

1.1k views Asked by At

I have written the following Midje test:

(fact (followers-minus-friends ...name...) => ["Dude"]
      (provided (idset show-followers ...name...) => #{1 2 3}
                (idset show-friends ...name...) => #{1 2}
                (userinfos #{3}) => [{:screen_name "Dude"}]))

to test the following function (in a different namespace):

(defn followers-minus-friends [screenname]
  (let [difference-ids (difference (idset show-followers screenname)
                                   (idset show-friends screenname))
        userinfos (userinfos difference-ids)]
    (map :screen_name userinfos)))

The test may seem pretty useless, but I'm just trying to get accustomed to Midje. Somehow, the function idset just gets executed, which I wanted to prevent by providing a return value in the provided-clause. What could be an explanation for this?

EDIT: I have uploaded the project to Github here, in case you want to try to reproduce the above situation: https://github.com/Borkdude/twitter-utils

1

There are 1 answers

0
Brian Marick On BEST ANSWER

The problem is that the function idset is private in your source namespace. In the test file, you intern it in the test namespace. You now have two different vars that point to the same function:

 #'twitter-utils.core/idset
 #'twitter-utils.test.core/idset

In the provided, you are overriding the second var to point to a prerequisite function. However, the function followers-minus-friends still refers to the first var. That is not overridden, so you get the original function.

A common idiom for routing around private declarations is to use the full var name, as above. So you'd expect this to work:

(fact (followers-minus-friends ...name...) => ["Dude"]
      (provided (#'twitter-utils.core/idset show-followers ...name...) => #{1 2 3}
                (#'twitter-utils.core/idset show-friends ...name...) => #{1 2}
                (userinfos #{3}) => [{:screen_name "Dude"}]))

It doesn't, though. I consider that a bug in Midje. At least, I can't offhand see what harm supporting that would do. I'll file a ticket.

As it stands, you need to make idset public.