https://github.com/jechol/mok
Function mock generator for Elixir
https://github.com/jechol/mok
Last synced: 12 months ago
JSON representation
Function mock generator for Elixir
- Host: GitHub
- URL: https://github.com/jechol/mok
- Owner: jechol
- License: other
- Created: 2022-02-23T08:22:14.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-05-02T16:53:01.000Z (about 2 years ago)
- Last Synced: 2025-06-11T05:58:39.040Z (about 1 year ago)
- Language: Elixir
- Homepage:
- Size: 325 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
[](https://github.com/jechol/mok/actions)
[](https://hex.pm/packages/mok)
[](https://github.com/jechol/mok/blob/main/LICENSE.md)
`mok` is small library to inject and mock functions easily.
## Installation
The package can be installed by adding `mok` to your list of dependencies
in `mix.exs`:
```elixir
def deps do
[{:mok, "~> 0.1"}]
end
```
## Usage
#### `inject`, `mock`
```elixir
defmodule Target do
require Mok
def call(mocks) do
a = [1, 2, 3] |> List.first() |> Mok.inject(mocks, "unmatched") # 1 (not injected)
b = [1, 2, 3] |> List.first() |> Mok.inject(mocks, "matched") # 10 (injected)
c = [1, 2, 3] |> List.first() |> Mok.inject(mocks, nil) # 1 (not injected)
d = [1, 2, 3] |> List.first() |> Mok.inject(mocks) # 100 (injected)
a + b + c + d
end
end
```
```elixir
require Mok
assert 4 == Target.call(%{})
assert 112 ==
Target.call(
Mok.mock(%{
{&List.first/1, "matched"} => 10,
&List.first/1 => 100
})
)
```
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE.md) file for details