module_mocker v0.2.0 ModuleMocker

ModuleMocker helps to mock a module during testing. ModuleMocker does not mock anything for the test, it just provide convention for mocking the module. ModuleMocker should be used to mock modules taht acesses external api or do other heavy lifting work needs to be mocked. You are responsible for defining the mock module and ModuleMocker will just provide easy way to use it and provide convention to define mock module instead of manually configuring it. Check this article on Mocking to have an idea of how ModuleMocker can be used. We can pass second parameter to give custom name to module attribute.

Note: ModuleMocker may not work with alias. It accepts fully qualified name of module to be mocked

Summary

Macros

Adds module atribute for the module passed in as argument. If module A.B.C is passed to the function then it will set module attribute ‘@c’ to A.B.C in development and production and ‘@c’ to A.B.Mock.C in test. Last part of the fully qualified name of the module will used as the module attribute name by default. Last part of module name would be converted to underscore format to create a module attribute. i.e) mock_for_test MyModule.SomeModule will create module attribute with name ‘@some_module’

Macros

mock_for_test(arg, module_attribute \\ nil)

Adds module atribute for the module passed in as argument. If module A.B.C is passed to the function then it will set module attribute ‘@c’ to A.B.C in development and production and ‘@c’ to A.B.Mock.C in test. Last part of the fully qualified name of the module will used as the module attribute name by default. Last part of module name would be converted to underscore format to create a module attribute. i.e) mock_for_test MyModule.SomeModule will create module attribute with name ‘@some_module’

Example:

def MyModule.MyAuth
  def func do
    IO.puts "Original function func"
  end
end

def MyModule.Mock.MyAuth
  def func do
    IO.puts "Mock function func"
  end
end

defmodule M do
  import ModuleMocker
  mock_for_test MyModule.MyAuth

  def func do
    @my_auth.func
  end
end

M.func will return “Original function func” in development and production and Will return “Mock function func” in testing environment

Note: You have to define origin module along with mock module yourself. ModuleMocker just establishes convention.