How to check that method of a mockobject was not called only with a specific parameter?

k0pernikus

I have a PHPUnit_Framework_MockObject_MockObject of a Logger.

In a unit test, I do not want a call to the warn method to happen with a specific string parameter doNotCallMeWithThisString.

I have come this far:

public function testThis()
{
    ...

    $logger = $this->getMockLogger();
    $logger->expects($this->exactly(0))->method('warn')->with(
        $this->equalTo('doNotCallMeWithThisString')
    );
    ...
}

Yet this fails because exactly(0) marks any call to the warn method as an error even is the string parameter is somethingEntirelyUnrelated.

How can I tell the mock object that any call to warn is fine unless it is called with that specific string?

Schleis

the exactly() method is for asserting how many times an mocked method will be called. Unless you are using $this->at() for the mocked behavior, you don't specify the arguments for a specific call. exactly(0) says that the number of calls should be 0.

Change your mock to this:

 $logger->expects($this->any()) //Or however many times it should be called
        ->method('warn')
        ->with(
              $this->callback(function($argument) {
                  return $argument !== 'doNotCallMeWithThisString';
              })
         )
 );

This uses a callback to check the argument passed to the mocked method and validates that it isn't equal to your string.

The PHPUnit documentation has the constraint types that you can use to verify the arguments of a mock. At this time, it doesn't have a string not equals type. But using the callback, you can make your own. Your callback just needs to check the argument used and returns true if it is ok and false if it isn't.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to ensure that a method can only be called from a specific dll

From Java

Mockito: verify that a method was not called with specific parameter type

From Dev

Mockito - verify method is called with a specific parameter ( condition )

From Dev

How to check if member method is called

From Dev

How to test if a specific method is called

From Dev

How can I make sure that a method of class can be called from a specific class only?

From Dev

How to check if jQuery, $ is called with a specific selector in Jasmine?

From Dev

How to check if a certain method has been called?

From Dev

In testing in java how to check if a method of an interface is called or not?

From Dev

How to check wether or not a method is called by a child?

From Java

How to verify that a specific method was not called using Mockito?

From Dev

Spock Test, only check if method is called and do not execute it

From Dev

Check if method was called on EasyMock

From Dev

How to check that class method require parameter

From Dev

how to find which method called a specific method with break point in it

From Dev

How to implement only specific method of CrudRepository in Spring?

From Dev

Java - How to check if dynamically instantiated method is called at runtime?

From Dev

SpringSecurity check method parameter

From Dev

How to display all methods that are called within a specific method in Eclipse Java?

From Dev

Testing method called with specific string

From Dev

Only base method of generic called

From Dev

method onFocusChangeListener called only once

From Dev

Factory method only called once

From Dev

Factory method only called once

From Dev

paintComponent method is called only once

From Dev

Check whether method is called directly or by another method

From Dev

How to check if method parameter type/return type is generic, in Roslyn?

From Dev

How to set only specific values for a parameter in docopt python?

From Dev

How to pass a non static method from a specific class as parameter to Function

Related Related

  1. 1

    How to ensure that a method can only be called from a specific dll

  2. 2

    Mockito: verify that a method was not called with specific parameter type

  3. 3

    Mockito - verify method is called with a specific parameter ( condition )

  4. 4

    How to check if member method is called

  5. 5

    How to test if a specific method is called

  6. 6

    How can I make sure that a method of class can be called from a specific class only?

  7. 7

    How to check if jQuery, $ is called with a specific selector in Jasmine?

  8. 8

    How to check if a certain method has been called?

  9. 9

    In testing in java how to check if a method of an interface is called or not?

  10. 10

    How to check wether or not a method is called by a child?

  11. 11

    How to verify that a specific method was not called using Mockito?

  12. 12

    Spock Test, only check if method is called and do not execute it

  13. 13

    Check if method was called on EasyMock

  14. 14

    How to check that class method require parameter

  15. 15

    how to find which method called a specific method with break point in it

  16. 16

    How to implement only specific method of CrudRepository in Spring?

  17. 17

    Java - How to check if dynamically instantiated method is called at runtime?

  18. 18

    SpringSecurity check method parameter

  19. 19

    How to display all methods that are called within a specific method in Eclipse Java?

  20. 20

    Testing method called with specific string

  21. 21

    Only base method of generic called

  22. 22

    method onFocusChangeListener called only once

  23. 23

    Factory method only called once

  24. 24

    Factory method only called once

  25. 25

    paintComponent method is called only once

  26. 26

    Check whether method is called directly or by another method

  27. 27

    How to check if method parameter type/return type is generic, in Roslyn?

  28. 28

    How to set only specific values for a parameter in docopt python?

  29. 29

    How to pass a non static method from a specific class as parameter to Function

HotTag

Archive