A
A
Alexander2018-01-23 20:31:32
PHP
Alexander, 2018-01-23 20:31:32

How to write a test for a non-existent method?

Greetings.
There is a simplified abstract class that calls the inheritor method:

AbstractClass
<?php
namespace Foo\Bar;

abstract class AbstractClass
{
    public function trigger($event)
    {
        if ($event && method_exists($this, $event)) {
            return $this->"on".$event();
        }
    }
}

How to write a test that will check trigger("Test"), i.e. calling a non-existent onTest method?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@smple, 2018-01-24
@codemafia

well, I’ll answer for an example that in the current situation there is no
reason why this should not be done
1. Unit test it covers a SPECIFIC instance of the class, not an abstract one (therefore, it will be necessary to write unit tests for all NOT abstract classes).
2. It is not possible to write a unit test for an abstract class, since it is impossible to create an instance of an abstract class. Therefore, given the above, it is necessary to test concrete classes that inherit this class. Well, and calling like this, possibly existing methods, is not a particularly good practice, I would probably write something like this differently, but this does not apply to testing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question