简体   繁体   中英

PHPUnit, mock constructor?

I want to mock the constructor like any other methods. I also added a willReturnCallback clause, which does not seem to work. I mean, it all works with methods, but not with the constructor.

$mock = $this->getMock ('MyClass', array(), array(), '', false);
$mock->expects($this->once())->method('__construct')->willReturnCallback(function() { echo 'outputt'; });

so mocking constructor has no effect.

Adding the relevant comments as community wiki answer because there will never be another answer than "not logically possibly":

The original constructor is called during mock construction (it just calls the baseclass' constructor). If you don't want that, you can also disable it. However, I wonder why you want to mock the constructor? The point is that unless you explicitly call it (which would be a code smell), creating the mock does the construction, so it's too late to expect a constructor call

Besides all limitations of mocking, a controller constructor can never return anything, so the mock doesn't mean anything at the moment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM