This is an example Sinatra app that uses both dry-container
and dry-auto_inject
to manage dependencies and allow for
swapping them at test time.
The application itself is simple -- it's a Sinatra app that exposes a single endpoint that proxies requests to a configured external host. These parts best highlight the usage of dependency injection:
- Dependencies are declared in
Container
for both theProxyClient
and environment variables that will be used in the main application - The dependency for the proxy client is automatically injected into
App
withdry-auto_inject
- The test environment specifies a fake
(
ProxyClientFake
) with the same interface asProxyClient
that can be swapped in at test time - Testing uses
enable_stubs!
on the container to allow swapping in the fake proxy client for faking the connection to the external service
Install the dependencies and start the server
bundle && rackup
Test the proxy endpoint:
curl -s http://localhost:9292/proxy/get | python3 -m json.tool
You can run the integration tests with TLDR:
bundle exec tldr