Mock frameworks returns class with different name and type
mockery, mocking, php, phpunit, unit-testing
Solution
I just experimented with an existing test of my own, and by changing the interface namespace name from one that exists to one that doesn't exist, I got exactly the same as what you describe (using phpunit). My mock object had the class name `Mock_ViewInterface_c755461e`. When I change 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 doesn't exist (e.g. a typo or missing namespace component).
- Your library code isn't being loaded for some reason, e.g. autoloading is not setup correctly in your unit test bootstrap.
Problem
I'm trying to create a mock to satisfy a typehint with this code (Mockery): ``` return \Mockery::mock('\Contracts\Helpers\iFileSystemWrapper'); ``` or this (PHPUnit): ``` return $this->getMock('\Contracts\Helpers\iFileSystemWrapper'); ``` But the mock returned is called `Mockery\Mock Object` or `Mock_iFileSystemWrapper_a5f91049`. How am I supposed to type check this when it isn't an instance of what I need at all with either framework? Why exactly is the mock framework trying to load the real class? If I wanted the real class I would include the real class. This problem has slowed me down so many times when writing tests I'm about to just toss type hinting out the window and check class names instead, or simply use production objects as mocks are a pain to use.