Posted on Wednesday, 8th October 2008 by charpi
In my previous post, I wrote a test to specify a
mock. In my mind, a lazy mock consists to replace the implementation
of one module by another one at runtime.
Here is the implementation of the
-module(mock). -export([replace_module /2]). replace_module (Module, Mock_module) -> uninstall (Module), {ok, Binary} = file: read_file (code: which (Mock_module)), File_name = atom_to_list (Module) ++ ".erl", code: load_binary(Module, File_name, Binary), ok. uninstall (Module) -> code: purge (Module), code: delete (Module).
The tricky thing is that your
erlang beam file. The code_loader use the beam file name to locate the
code load but the VM expected that the module declaration in the file
got the same name than the file.
In order to use my implementation of the
module must have the same module declaration than the real
implementation.
Here is the content of the source file my_mock_module.erl:
-module(my_module). -export([who /0]). who () -> "I'm the mocked code".
Tags: erlang, mock object
Posted in Uncategorized | Comments (0)
