Fork me on GitHub
#testing
<
2022-02-22
>
zeddan09:02:19

I’m writing tests for an existing application running pedestal and carmine and I’m trying to override/set a variable in a test, but it seems it doesn’t have any effect. The error is java.lang.IllegalArgumentException: hostname can't be null which makes sense due to the way we read the config file, so I want to mock the value. The implementation:

(def service
  {...
   ::http/enable-session {:cookie-name "abc"
                          :store (taoensso.carmine.ring/carmine-store {:pool {} :spec {:host (:host (cnf/carmine))}} {:key-prefix "abc:carmine:session" :expiration-secs (* 60 60 12)})}
   ...})
The config edn file:
:carmine {:host "127.0.0.1"}
The test:
(deftest service-test
  (with-redefs [cnf/carmine {:host "127.0.0.1"}]
    (is (= 200 (:status (response-for service :get "/ping"))))))
Does anyone have any idea why the hostname is still null?

vemv11:02:03

with-redefs doesn't change what has already been defined with def

zeddan12:02:29

Okay, in the config it is defined with defn like this (defn carmine [] (:carmine @config-atom)) but maybe that doesn’t matter?

zeddan12:02:05

How could I set the value in the test?

zeddan12:02:43

Or maybe I’m approaching it the wrong way? Should i set up the entire carmine service in the test?

vemv12:02:46

If your service actually looks like this: def service it doesn't matter, with-redefs will be unable to have an effect

vemv12:02:57

> Or maybe I’m approaching it the wrong way? I'd say so, most people use Component/Integrant/... plus a config lib like Aero. with-redefs wouldn't be used - instead you'd make a mock Component implementation for the Carmine service, that exists only within tests

vemv12:02:33

If that doesn't sound familiar to you perhaps you can try out a framework/boilerplate template and borrow inspiration from there :)

zeddan12:02:35

I’ll look into it, thanks for pointing in the right direction 🙂

🍻 1