Sergey Sergey - 1 year ago 116
PHP Question

Test the code that doesn't exist

TDD claims that I should write tests first. Say I want to write a service, how do I start writing tests for the service if I don't have anything yet at all? What would be the first test? An attempt to instantiate the service and get an exception?

PHP Laravel-based example:

class ServiceTest extends TestCase
public function testServiceExists()
$service = App::make('grid');


PHPUnit 5.5.4 by Sebastian Bergmann and contributors.

E 1
/ 1 (100%)

Time: 123 ms, Memory: 14.00MB

There was 1 error:

1) ServiceTest::testServiceExists ReflectionException: Class grid does
not exist


ERRORS! Tests: 1, Assertions: 0, Errors: 1.

Answer Source

Encountering such errors is a good thing in TDD. It's implying that you need to resolve the error just in order to encounter another one.

When coding in TDD, you program by wishful thinking. In other words, you write some code that uses a component, before you implement the component itself. This helps to discover what functions and data you need, and following this discovery will lead you to more simple and useful APIs.

Even though the TDD concepts are not very hard to learn, getting used to TDD's test-first development approach is hard and time-consuming. You're on the right track, just read more and test more to become comfortable with the methodology.

As you're using Laravel, I strongly recommend you to have a look at Laracasts' series on testing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download