Fork me on GitHub
#integrant
<
2021-02-11
>
richiardiandrea17:02:38

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

sandqvist18:02:12

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.

richiardiandrea19:02:49

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

richiardiandrea19:02:53

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)

richiardiandrea19:02:03

still curious though on how to use derive for swapping dependencies

sandqvist19:02:06

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

sandqvist19:02:00

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.

sandqvist19:02:05

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.

sandqvist19:02:45

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

richiardiandrea19:02:07

ok I see thank you

richiardiandrea19:02:35

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

richiardiandrea19:02:18

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

sandqvist18:02:49

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.