integrant

richiardiandrea 2021-02-11T17:38:38.039500Z

Hi all, I am new to integrant and try to replace a ref with a mock implementation I want to provide in the test... The mock dep is built in a let like:

diagnostic-report-repo (shrub/stub repository/Repository
                                     {:-one nil})
And what I'd like to do is to set it as "already" initialized dep

sandqvist 2021-02-11T18:48:12.044900Z

It's too late to do that after you have initialized your system as references to the actual implementation will not be affected. This is assuming you are using ig/ref in your system map. I would recommend setting up your system map correctly (before calling ig/init). You can use https://github.com/weavejester/integrant#derived-keywords so that Integrant uses the test component when the actual component is not in your system map.

sandqvist 2021-02-11T18:50:49.046900Z

I have somewhat complex system maps and use https://github.com/juxt/aero to generate the variations. You just have to write a reader tag implementation which calls ig/ref and you can describe your system structure in an edn file.

richiardiandrea 2021-02-11T19:08:49.048400Z

thank you for the hint...I was thinking about maybe

(ig/init-key :com.cohesic.acuity.infra2.reporting-service/impl
                                    {:diagnostic-report-repository diagnostic-report-repo})
Would that be a good idea? it's kind of bypassing resolution

richiardiandrea 2021-02-11T19:12:53.048700Z

I am leaning towards a yes, it makes for a very simple way of unit testing (if I had to "integration test" I would definitely go for aero and a proper test.edn config)

richiardiandrea 2021-02-11T19:14:03.048900Z

still curious though on how to use derive for swapping dependencies

sandqvist 2021-02-11T19:27:06.049100Z

You can init one component with init-key if you have no dependencies but I might misunderstand your intent here.

sandqvist 2021-02-11T19:29:00.049300Z

With derive you can use (derive :test/impl :com.cohesic.acuity.infra2.reporting-service/impl) and replace the actual key-value -pair in your system map with :test/impl {config here} and just implement the test version as you would any component.

sandqvist 2021-02-11T19:32:05.049500Z

Another option would be something like

(derive :com.cohesic.acuity.infra2.reporting-service/impl :interface/reporting-service)
(derive :test-reporting-thing/impl :interface/reporting-service)
and just reference :interface/reporting-service in other components. I don't do this but it might be reasonable, especially if the components implement the same interface and it is used by components which depend on it.

sandqvist 2021-02-11T19:34:45.049900Z

But these are more for integration tests where you would have a mostly complete system.

richiardiandrea 2021-02-11T19:39:07.050100Z

ok I see thank you

richiardiandrea 2021-02-11T19:39:35.050300Z

the problem is the every test will have a different stub so I think I need init-key there

richiardiandrea 2021-02-11T19:43:18.050500Z

but thanks a lot this is very helpful - I there is definitely some different in thinking around this compared to component or mount