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?with-redefs doesn't change what has already been defined with def
Okay, in the config it is defined with defn like this (defn carmine [] (:carmine @config-atom)) but maybe that doesn’t matter?
How could I set the value in the test?
Or maybe I’m approaching it the wrong way? Should i set up the entire carmine service in the test?
If your service actually looks like this: def service it doesn't matter, with-redefs will be unable to have an effect
Allright
> 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
If that doesn't sound familiar to you perhaps you can try out a framework/boilerplate template and borrow inspiration from there :)
I’ll look into it, thanks for pointing in the right direction 🙂