无法通过Midje模拟HugSQL生成的数据库功能

时间:2019-02-13 07:23:31

标签: clojure midje hugsql

在使用Conman帮助程序库模拟从HugSQL模板生成的数据库访问函数时遇到问题。这些函数在运行时定义如下:

(conman/bind-connection *db* file)

就我而言,我正在对该服务功能进行单元测试:

(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])))

其中Conman生成db/create-user!函数。但是,当尝试使用Midje测试模拟该功能时,出现以下错误消息:

(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]

我从服务和测试中完全相似地引用了动态创建和模拟的函数,但是midje不能将其识别为同一函数。我还尝试通过var(#'musician.user.db/create-user!)引用该函数,并使用服务和测试中的完整路径musician.user.db/create-user!,但两者均无效。通过REST API和REPL进行测试时,服务代码可以正常工作,我只是无法设置此测试。

问题似乎是midje无法模拟该函数,因为在运行时之前未知。这仅仅是正确引用该函数的问题,还是无法用Midje模拟这些功能?

0 个答案:

没有答案