Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add meck:received/3 API for easier history checking #23

Merged
merged 3 commits into from
Mar 30, 2011

Conversation

mbbx6spp
Copy link
Contributor

Instead of the unit tester needing to parse the meck:history/1 output for a module and pattern matching for successful or exceptional calls for an {M, F, A} triple inside the test code, the addition of meck:received/3 function makes it easier to read the received calls expectations.

An example might be if you have a CRUD/REST API wrapper around a low-level datastore library, where you want to make sure that on an edit will first read the object/record about to be edited to first check for conflicts (assume you use a read-conflict datastore like CouchDb, Riak, etc.) the unit tester wants to know that the lowleveldb module received a call to read with specific arguments when calling the nicehighlevelapi:edit/1 function:

ensure_object_read_before_edit_test() ->
    ok = meck:new(nicehighlevelapi, [passthrough]),
    Key = "MyUniqueKey",
    _Result = nicehighlevelapi:edit(Key),
    ?assertEquals(true, meck:received(nicehighlevelapi, edit, [Key])),
    ?assert(meck:validate(nicehighlevelapi).

Eventually I would like to add more ways to do this via the received/3 or received/Other when Other =/= 3 function(s) to offer APIs equivalent to what I am used to using Ruby's RSpec.

I can also add this as a wrapper library around Meck, if you disagree with this the concept of adding this to meck. Thoughts and suggestions are welcome.

The purpose of meck:receive/3 is to provide those writing tests to write
cleaner unit test code such that they can easily declare what calls should
have been received on a module with arguments like so:
  mymod_test_test() ->
    ok = meck:new(mymod),
    ok = meck:expect(mymod, test, 0, ok),
    %% call some public API
    ?assertEquals(true, meck:received(mymod, test, [])),
    ?assert(meck:validate(mymod)).

This way the unit tester does not need to know the format of history/1 entries.

This is similar to basic mock received expectations seen in Ruby libraries
like RSpec mocks/stubs, e.g.:
  myclass.should_receive(:method1).with(Arg1, Arg2)

This is only the tip of the iceberg as to what writing cleaner received
expectations for unit testing code, especially when compared to RSpec's API.
@eproxus
Copy link
Owner

eproxus commented Mar 25, 2011

This is an idea I've been toying with as well for some time. Just never got the time to sit down and flesh it out.

Put some comments on your check-in, ready to pull when you've updated the code.

Great work on the code and tests!

Changes suggested by eproxus on GitHub for merging into canonical meck
repository that are made in this commit are:
* Fix EDoc spec typo: s/Arity::int()/Args::list(term())/
* Repalce use of deprecated bool() to boolean() in EDoc spec
* Replace use of deprecated bool() to boolean() in spec annotation
* Update @doc summary text to eproxus' suggestion for clarity
* Rename received function name to called
* Rename has_call_info function name to has_been_called
* Placed meck:history(Mod) call inline of has_been_called call
@mbbx6spp
Copy link
Contributor Author

I think I added all the changes. There is now a second commit in this pull request. Let me know if I missed anything.

This commit includes:
 * Renaming of private function name from has_been_called to has_call
with same arguments. This is in reference to the GitHub pull request
(eproxus#23) commit comment by @eproxus.
@eproxus eproxus merged commit 735856b into eproxus:master Mar 30, 2011
@eproxus
Copy link
Owner

eproxus commented Mar 30, 2011

I've merged into master now. Thanks for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants