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

Feature/issue 391 add should not receive expectation #430

Merged
merged 5 commits into from Feb 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/reference/expectations.rst
Expand Up @@ -41,6 +41,13 @@ a set of operations which are recorded as expectations on the partial mock. A
simple use case is automatically recording expectations based on an existing
usage (e.g. during refactoring). See examples in a later section.

.. code-block:: php

shouldNotReceive(method_name)

Declares that the mock should not expect a call to the given method name. This
method is a convenience method for calling ``shouldReceive()->never()``.

.. code-block:: php

with(arg1, arg2, ...) / withArgs(array(arg1, arg2, ...))
Expand Down
13 changes: 13 additions & 0 deletions library/Mockery/Mock.php
Expand Up @@ -202,6 +202,19 @@ public function shouldReceive()
return $lastExpectation;
}

/**
* Shortcut method for setting an expectation that a method should not be called.
*
* @param mixed
* @return \Mockery\Expectation
*/
public function shouldNotReceive()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this needs to be added to MockInterface as well.

{
$expectation = call_user_func_array(array($this, 'shouldReceive'), func_get_args());
$expectation->never();
return $expectation;
}

/**
* Allows additional methods to be mocked that do not explicitly exist on mocked class
* @param String $method name of the method to be mocked
Expand Down
8 changes: 8 additions & 0 deletions library/Mockery/MockInterface.php
Expand Up @@ -40,6 +40,14 @@ public function mockery_init(\Mockery\Container $container = null, $partialObjec
*/
public function shouldReceive();

/**
* Shortcut method for setting an expectation that a method should not be called.
*
* @param mixed
* @return \Mockery\Expectation
*/
public function shouldNotReceive();

/**
* Allows additional methods to be mocked that do not explicitly exist on mocked class
* @param String $method name of the method to be mocked
Expand Down
27 changes: 27 additions & 0 deletions tests/Mockery/ExpectationTest.php
Expand Up @@ -400,6 +400,33 @@ public function testThrowsExceptionOnNoArgumentMatch()
public function testNeverCalled()
{
$this->mock->shouldReceive('foo')->never();
$this->container->mockery_verify();
}

public function testShouldNotReceive()
{
$this->mock->shouldNotReceive('foo');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you be calling $this->container->mockery_verify(); here as well and verify, that if more than 0 calls are made we're getting an exception?

$this->container->mockery_verify();
}

/**
* @expectedException \Mockery\Exception\InvalidCountException
*/
public function testShouldNotReceiveThrowsExceptionIfMethodCalled()
{
$this->mock->shouldNotReceive('foo');
$this->mock->foo();
$this->container->mockery_verify();
}

/**
* @expectedException \Mockery\Exception\InvalidCountException
*/
public function testShouldNotReceiveWithArgumentThrowsExceptionIfMethodCalled()
{
$this->mock->shouldNotReceive('foo')->with(2);
$this->mock->foo(2);
$this->container->mockery_verify();
}

/**
Expand Down