testing

zeddan 2022-02-22T09:58:19.677639Z

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?

vemv 2022-02-22T11:45:03.226869Z

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

zeddan 2022-02-22T12:03:29.522199Z

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

zeddan 2022-02-22T12:04:05.332929Z

How could I set the value in the test?

zeddan 2022-02-22T12:05:43.252969Z

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

vemv 2022-02-22T12:08:46.468699Z

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

zeddan 2022-02-22T12:10:29.670019Z

Allright

vemv 2022-02-22T12:10:57.084579Z

> 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

vemv 2022-02-22T12:11:33.873449Z

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

zeddan 2022-02-22T12:20:35.296359Z

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

🍻 1