1

I'm having a problem mocking database access functions generated from HugSQL templates with the Conman helper library. These functions are defined at runtime like this:

(conman/bind-connection *db* file)

In my case, I'm unit testing this service function:

(defn create-user [user]
  (do (-> user
          (password-service/assoc-salt-and-hash)
          (dissoc :password)
          (assoc :is-admin false)
          (db/create-user!))
      (select-keys user [:firstname :lastname :email])))

In which the db/create-user! function is generated by Conman. However, when trying to mock out that function with this Midje test, I'm getting the following error message:

(fact "calls db function with created password fields and without plaintext password"
               (create-user user) => (select-keys user [:firstname :lastname :email])
               (provided
                 (password/assoc-salt-and-hash (as-checker (contains user))) => (merge user salt-and-hash)
                 (db/create-user! (as-checker (contains user-to-db))) => user-to-db))
FAIL at (user.clj:24)
You never said #'create-user! would be called with these arguments:
    [{:firstname "asd", :lastname "lol", :email "email@email.com", :password-salt #object["[B" 0x5074bde0 "[B@5074bde0"], :password-hash #object["[B" 0x60f8a634 "[B@60f8a634"], :is-admin false}]

FAIL at (user.clj:25)
These calls were not made the right number of times:
    (db/create-user! (as-checker (contains user-to-db))) [expected at least once, actually never called]
nil
FAILURE: 2 checks failed.  (But 1 succeeded.)
[Completed at 20:27:05]

I'm referencing the dynamically created and mocked function exactly similarly from both the service and the test, but midje doesn't recognize it as being the same function. I also tried referencing the function via the var (#'musician.user.db/create-user!), and using the full path musician.user.db/create-user! from both the service and test, but neither of those worked either. The service code does work when tested from the REST API and the REPL, I just can't get this test set up.

It seems that the problem is that midje is unable to mock the function as it is not known before runtime. Is this merely a problem of referencing the function correctly, or it is just not possible to mock out these funtions with Midje?

Kristian Salo
  • 350
  • 3
  • 9
  • 1
    No, I think you're on the wrong track. The error says that midje saw a call to `db/create-user!` with arguments while executing your `create-user` function that doesn't match what you're describing in your `provided` clause. As a first attempt to fix this, I would try replacing the `as-checker` statement with `anything`. Then you need work backwards to fix your prerequisite checker. – schaueho Feb 13 '19 at 07:44

0 Answers0