snivilised / cobrass

🐲 Assistant for cli applications using cobra
https://pkg.go.dev/github.com/snivilised/cobrass
MIT License
1 stars 0 forks source link

create viper mock #243

Closed plastikfan closed 8 months ago

plastikfan commented 8 months ago

Need to start using mocking, in this case so we can mock out viper config. Using uber mock.

The first to know, is how to make a mock; here is the documented example (as described in mock_test.go):

mockgen -destination mock_test.go -package gomock_test -source example_test.go

Repository=MockSensorRepository,Endpoint=MockSensorEndpoint, where Repository is the interface name and MockSensorRepository is the desired mock name

But actually, this doesnt clearly explain how to mock a foreign interface. I found this: codecentric.de gomock-tutorial, which may be a bit clearer, in particular:

mockgen -destination=mocks/mock_doer.go -package=mocks github.com/sgreben/testing-with-gomock/doer Doer

where:

plastikfan commented 8 months ago

The following command generated a mock for VIperConfig (run from the repo root dir):

mockgen -destination src/assistant/mocks/mock-global-viper-config.go -package mocks -source src/assistant/configuration/global-config.go -mock_names ViperConfig=MockViperConfig

Since we want mocks to be used externally, we generate the mocks inside a mocks package, not mocks_test.

A new task co-gen-vc has been added and a go generate statement added to global-config.go