One way to solve this problem is to embed the Dependency Injection module into the database.
You define your status record as
-record(state, { ..., db_mod }).
And now you can add db_mod to init / 1 gen_server:
init([]) -> {ok, DBMod} = application:get_env(my_app, db_mod), ... {ok,
So, when we have the code:
statename(save_data, _From,
We have the opportunity to override the database module when testing with another module. Implementing a stub is now quite easy, and you can thus change the way the database code is presented as you see fit.
Another alternative is to use a tool like meck to mock the database module during testing, but I usually prefer to tune it.
In general, I usually break code that is complex into my own module, so it can be tested separately. I rarely do many unit tests of other modules and prefer large-scale integration tests to handle errors in such detail. Take a look at Common Test, PropEr, Triq and Erlang QuickCheck (the latter is not open source and is not a full version).
source share