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

Projects
None yet
2 participants
@mbbx6spp
Contributor

mbbx6spp commented Mar 24, 2011

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.

Add meck:received/3 helper to wrap meck:history/1
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

This comment has been minimized.

Show comment
Hide comment
@eproxus

eproxus Mar 25, 2011

has_call is a better name?

eproxus commented on src/meck.erl in f1e0022 Mar 25, 2011

has_call is a better name?

This comment has been minimized.

Show comment
Hide comment
@mbbx6spp

mbbx6spp Mar 25, 2011

Owner

has_been_called?

Owner

mbbx6spp replied Mar 25, 2011

has_been_called?

This comment has been minimized.

Show comment
Hide comment
@eproxus

eproxus Mar 26, 2011

I prefer the minimalist approach.

true = has_call(Call, History)

I guess it's a matter of taste. :-) (the extreme version would be has(Call, History) but that's maybe too extreme...)

eproxus replied Mar 26, 2011

I prefer the minimalist approach.

true = has_call(Call, History)

I guess it's a matter of taste. :-) (the extreme version would be has(Call, History) but that's maybe too extreme...)

This comment has been minimized.

Show comment
Hide comment
@mbbx6spp

mbbx6spp Mar 29, 2011

Owner

Missed this. I will commit shortly.

Owner

mbbx6spp replied Mar 29, 2011

Missed this. I will commit shortly.

@eproxus

This comment has been minimized.

Show comment
Hide comment
@eproxus

eproxus Mar 25, 2011

I would change to:

%% @doc Returns whether `Mod:Func` has been called with `Args`.

eproxus commented on src/meck.erl in f1e0022 Mar 25, 2011

I would change to:

%% @doc Returns whether `Mod:Func` has been called with `Args`.
@eproxus

This comment has been minimized.

Show comment
Hide comment
@eproxus

eproxus Mar 25, 2011

Arity::int() should be Args::list(term())

eproxus commented on src/meck.erl in f1e0022 Mar 25, 2011

Arity::int() should be Args::list(term())

This comment has been minimized.

Show comment
Hide comment
@mbbx6spp

mbbx6spp Mar 25, 2011

Owner

Doh:), I will also change bool() (now deprecated) to boolean().

Owner

mbbx6spp replied Mar 25, 2011

Doh:), I will also change bool() (now deprecated) to boolean().

@eproxus

This comment has been minimized.

Show comment
Hide comment
@eproxus

eproxus Mar 25, 2011

meck:history(Mod) can be placed inline in the has_call_info/2 call instead.

eproxus commented on src/meck.erl in f1e0022 Mar 25, 2011

meck:history(Mod) can be placed inline in the has_call_info/2 call instead.

@eproxus

This comment has been minimized.

Show comment
Hide comment
@eproxus

eproxus Mar 25, 2011

Makes more sense to be called called instead, I think.

eproxus commented on src/meck.erl in f1e0022 Mar 25, 2011

Makes more sense to be called called instead, I think.

@eproxus

This comment has been minimized.

Show comment
Hide comment
@eproxus

eproxus Mar 25, 2011

Owner

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!

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!

@mbbx6spp

This comment has been minimized.

Show comment
Hide comment
@mbbx6spp

mbbx6spp Mar 25, 2011

Owner

Will work on amendments while at #erlangfactory today. Are you here today?

Owner

mbbx6spp commented on f1e0022 Mar 25, 2011

Will work on amendments while at #erlangfactory today. Are you here today?

This comment has been minimized.

Show comment
Hide comment
@eproxus

eproxus Mar 26, 2011

Unfortunately not. Wish I was!

Looking forward to your contributions.

eproxus replied Mar 26, 2011

Unfortunately not. Wish I was!

Looking forward to your contributions.

Modified received/3 feature with changes suggested
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

This comment has been minimized.

Show comment
Hide comment
@mbbx6spp

mbbx6spp Mar 29, 2011

Contributor

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

Contributor

mbbx6spp commented Mar 29, 2011

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

Rename private function as per @eproxus suggestion
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
(#23) commit comment by @eproxus.

@eproxus eproxus merged commit 735856b into eproxus:master Mar 30, 2011

@eproxus

This comment has been minimized.

Show comment
Hide comment
@eproxus

eproxus Mar 30, 2011

Owner

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

Owner

eproxus commented Mar 30, 2011

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

@eproxus eproxus added the enhancement label Jun 29, 2017

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