1

我在使用 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])))

其中db/create-user!函数由Conman生成。但是,当尝试使用此 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 模拟这些函数?

4

0 回答 0