How To Unit Test MongoDB with Luminus?

341 Views Asked by At

I'm probably missing something simple, but I'm not experienced enough with clojure to understand the error I'm getting.

I have a simple luminus clojure app setup with the Monger library to handle my MongoDB connection. I've added a simple test that should always pass to my tests file.

(testing "create user"
    (let [result (db/create-user "test" "test" "test")]
    (is (true? true))))

This doesn't work however, and throws the following error

java.lang.ClassCastException: mount.core.DerefableState cannot be cast to com.mongodb.DB

I'm assuming this has to do with the test environment not properly setting up the database stuff and failing there. I've done a bit of searching, but nothing helpful has come up and there doesn't appear to be any testing help in the Monger Docs. I know for sure that the above create-user call does work when running in the actual environment.

1

There are 1 best solutions below

0
On

It seems like you're not binding your database before the tests run:

https://github.com/yogthos/clojure-error-message-catalog/blob/master/lib/mount/derefablestate-cannot-be-cast-to-ifn.md

I haven't used Luminus but I do have some integration tests for database related code in which I initialize my monger database connection ahead of time:

(ns ^:integration mango.db-test
(:require [clojure.test :refer :all]
        [monger.db :as mdb]
        [mango.db :as db]
        [mango.config :as config]))

(defn db-fixture [f]
  (println "Testing DB on " config/db-name)
  (db/init)
  (f)
  (mdb/drop-db @db/DB)
  (db/terminate)
)

(use-fixtures :once db-fixture)