Giter Site home page Giter Site logo

mox_env's Introduction

MoxEnv Hex.pm


It's your application config but simply mocked with Mox.


Usage

# myapp/lib/myapp/config.ex
defmodule MyApp.Config do
  def get(key, default \\ nil) do
    Application.get_env(:my_app, key, default)
  end
end

# myapp/test/support/config_mock.ex
defmodule MyApp.ConfigMock do
  use MoxEnv, config: MyApp.Config
end

# myapp/lib/myapp.ex
defmodule MyApp do
  @config Application.get_env(:my_app, :config_module, MyApp.Config)

  def test_key do
    @config.get(:test_key)
  end

  def test_key_default do
    @config.get(:test_key_default, :default_value)
  end
end

# config/test.exs
config :my_app, config_module: MyApp.ConfigMock

With that configuration you can simply mock your config in tests, like Mox:

iex> MyApp.test_key
:test_value

iex> MyApp.ConfigMock.put_env(:test_key, :new_value)
:new_value

And import some handy helpers into your test cases:

defmodule MyApp.ConnCase do
  use ExUnit.CaseTemplate

  # ...

  using do
    import Mox
    import MyApp.ConfigMock, only: [put_env: 2, put_env: 3, allow_env: 1, allow_env: 2]

    setup [:set_mox_from_context, :verify_on_exit!]
  end

  # ...
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

mox_env's People

Contributors

artemeff avatar devality avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

devality

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.