Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/BlakeWilliams/pact
Better dependency injection in Elixir
https://github.com/BlakeWilliams/pact
Last synced: about 1 month ago
JSON representation
Better dependency injection in Elixir
- Host: GitHub
- URL: https://github.com/BlakeWilliams/pact
- Owner: BlakeWilliams
- License: mit
- Created: 2015-01-05T23:28:09.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2018-09-10T17:04:42.000Z (over 6 years ago)
- Last Synced: 2024-10-31T10:06:27.608Z (about 1 month ago)
- Language: Elixir
- Size: 14.6 KB
- Stars: 76
- Watchers: 5
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- freaking_awesome_elixir - Elixir - Better dependency injection in Elixir for cleaner code and testing. (Miscellaneous)
- fucking-awesome-elixir - pact - Better dependency injection in Elixir for cleaner code and testing. (Miscellaneous)
- awesome-elixir - pact - Better dependency injection in Elixir for cleaner code and testing. (Miscellaneous)
README
# Pact
Pact is a dependency registry for Elixir to make testing dependencies easier.
## Why?Because testing Elixir dependencies could be a lot better. Why clutter up your
code injecting dependencies when a process can handle it for you?* You can declare your modules instead of passing them around like state.
* You can replace dependencies in a block context for easy testing.
* It makes your code cleaner.## Usage
In your application code:
```elixir
defmodule MyApp.Pact do
use Pactregister "http", HTTPoison
endMyApp.Pact.start_link
defmodule MyApp.Users do
def all do
MyApp.Pact.get("http").get!("http://foobar.com/api/users")
end
end```
In your tests:
```elixir
defmodule MyApp.UserTest do
use ExUnit.Case
require MyApp.Pacttest "requests the corrent endpoint" do
fakeHTTP = MyApp.Pact.generate :http do
def get!(url) do
send self(), {:called, url}
end
endMyApp.Pact.replace "http", fakeHTTP do
MyApp.Users.all
endassert_receive {:called, "http://foobar.com/api/users"}
end
end
```You can find more information in the [documentation].
[documentation]: http://hexdocs.pm/pact
## Disclaimer
Pact is very much an experiment at this point to see if it's viable. If you use
Pact please get in touch with me to let me know how it worked out for you or how
you think it could improve. If you have ideas feel free to open an issue or
create a pull request.