Skip to content

Commit

Permalink
Merge pull request #430 from robertbasic/feature/issue-391-add-should…
Browse files Browse the repository at this point in the history
…-not-receive-expectation

Feature/issue 391 add should not receive expectation
  • Loading branch information
davedevelopment committed Feb 18, 2015
2 parents f699a69 + e572f75 commit a6481d0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
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 @@ -204,6 +204,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()
{
$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');
$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

0 comments on commit a6481d0

Please sign in to comment.