Mock frameworks returns a class with a different name and type

I am trying to create a layout to suit the type type with this code (Mockery):

return \Mockery::mock('\Contracts\Helpers\iFileSystemWrapper'); 

or this (PHPUnit):

 return $this->getMock('\Contracts\Helpers\iFileSystemWrapper'); 

But the returned layout is called Mockery\Mock Object or Mock_iFileSystemWrapper_a5f91049 . How can I enter the type of check, if this is not an instance of what I need with any card at all?

Why is the mock framework trying to load a real class? If I needed a real class, I would include a real class.

This problem has slowed me down so many times when I write tests that I’m just going to throw in a type, hinting at a window, and instead check class names or just use production objects because mana is a pain to use.

+6
source share
2 answers

I was just experimenting with an existing test of my own, and changing the name of the interface namespace from existing to one that does not exist, I got exactly the same as what you described (using phpunit). My layout object had the class name Mock_ViewInterface_c755461e . When I get it back to the correct interface name, it works fine.

Therefore, I would say that either:

  • You are trying to use an interface name that does not exist (for example, a typo or a missing namespace component).
  • Your library code for some reason does not load, for example. autoload is not configured correctly in the unit test bootstrap.
+2
source

To check the base class, you need a special function. Something like that:

 $mock = $this->getMock('MyClass'); $this->assertInstanceOf('MyClass', $mock); 
-2
source

Source: https://habr.com/ru/post/921430/


All Articles