I'm testing a controller which possess an object that throws exception in it's constructor each time I do a test because the constructor check some environment variables aren't empty (but they are empty during the test). I tried mocking the class with getMockBuilder()->disableOriginalConstructor(), createMock, Mockery::mock()->makePartial(), I tried doing it in the setUp() before and after calling parent::setUp, I tried doing everything in the unit test instead of setUp(), they all execute the constructor and I don't understand why!
Here is an example of test:
public function setUp(): void
{
self::$mockClient = $this->createMock('App\Helpers\Client');
parent::setUp();
}
public function testSuccessfullyPost()
{
$input_data = [
'name' => 'valid name',
'email' => 'valid@email.com'
];
$this->json('POST', $this->testUri, $input_data, ['authentication' => $this->getToken()]);
$this->seeStatusCode(201);
}
Does anybody have an idea?
I am using phpUnit 9.5.10
I've used the following in the past:
$this->mockSchema = $this->getMockBuilder('Drupal\sqlsrv\Driver\Database\sqlsrv\Schema')
->setMethods(['getDefaultSchema', '__construct'])
->setMockClassName('MockSchema')
->setConstructorArgs([NULL])
->disableOriginalConstructor()
->getMock();
$this->mockSchema->method('getDefaultSchema')->willReturn('dbo');
If you disable the original constructor, I think you still need to “set the method” to prevent the parent constructor from executing.
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.