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

allows() and expects() syntax #668

Merged
merged 19 commits into from
Jan 27, 2017

Conversation

davedevelopment
Copy link
Collaborator

@davedevelopment davedevelopment commented Jan 17, 2017

Further to #598

expects() for setting up mock expectations

<?php

// old
$mock->shouldReceive("foo")->with(123)->andReturn(true)->once();
// or
$mock->shouldReceive()->foo(123)->andReturn(true)->once();

// new
$mock->expects()->foo(123)->andReturns(true);

allows() for stubbing methods

<?php

// old
$stub->shouldReceive("foo")->with(123)->andReturn(true);
// or
$stub->shouldReceive()->foo(123)->andReturn(true);

// new
$stub->allows()->foo(123)->andReturns(true);

Despite the new methods on the mocks, this is fully BC as they are skipped if the class to be mocked contains matching methods.

I personally prefer this syntax, but I haven't put it to much use. It's always bothered me that shouldReceive() starts with should, when it doesn't actually form a mock expectation without the additional call to ->once() or one of the other count methods.

My objectives here are not to remove the existing methods, but rather try and encourage the new methods going forward.

Todo

  • Basic docs in README
  • No args vs not caring about args

@davedevelopment
Copy link
Collaborator Author

davedevelopment commented Jan 20, 2017

Regarding my todo item on no args vs any args, there's a slight difference in behaviour between the old and the new syntax. Previously:

$double->shouldReceive("foobar")->andReturn(123);

$double->foobar(); // int(123)
$double->foobar(123); // int(123)
$double->foobar(true, "some string", 5.4); // int(123)

With the new syntax:

$double->allows()->foo()->andReturns(123);

$double->foo(); // int(123);
$double->foo(123); // Uncaught Exception Mockery\Exception\NoMatchingExpectationException

That is, when we say allows()->foo(), we really mean that's all we are allowing. To make it act like shouldReceive("foo"), we need to explicitly tell it withAnyArgs.

$double->allows()->bar()->withAnyArgs()->andReturns(123);

$double->bar(); // int(123);
$double->bar(123); // int(123)

I am fine with this, but I'm curious as to what other people think...

@coveralls
Copy link

coveralls commented Jan 27, 2017

Coverage Status

Coverage increased (+0.3%) to 77.315% when pulling 5ffcc2e on davedevelopment:allows-syntax into ae2c514 on padraic:master.

@davedevelopment davedevelopment changed the title WIP ->allows() and ->expects() syntax allows() and expects() syntax Jan 27, 2017
@coveralls
Copy link

coveralls commented Jan 27, 2017

Coverage Status

Coverage increased (+0.3%) to 77.315% when pulling d4bcc3d on davedevelopment:allows-syntax into ae2c514 on padraic:master.

@davedevelopment davedevelopment merged commit 85ccdd4 into mockery:master Jan 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants