0

I had little dispute with my coworkers about unit testing simple class. I have to test simple class like that.

class Person { /** @var string */ protected $name public function __construct(string $name) { $this->name = $name; } public function __toString(): string { return (string) $this->name; } } 

We both agree on testing toString method. But in my opinion, first we have to test creating this object as such:

public function testObjectCreation() { $testSubject = new Person('name'); $this->assertInstanceOf(Person::class, $testSubject); } 

My question is: Is there any sense in testing this case?

9
  • 5
    I doubt that you need this test. Commented Jan 25, 2019 at 14:57
  • 2
    In this literal example, or in general? There's no point in testing that PHP's __construct works, or that PHP can set properties, but if there's logic of any kind, then it certainly might be worth testing. It depends. This one specifically, no. Commented Jan 25, 2019 at 14:58
  • @JonStirling This is almost literal example. Im already convinced that this test is redundant. Commented Jan 25, 2019 at 15:02
  • 3
    By testing the __toString you'll be testing the constructor anyway Commented Jan 25, 2019 at 15:18
  • 2
    In TDD you could create a test for the creation of an object in an early stage. But it will be deleted when a new -and better- test covers this creation. Commented Jan 25, 2019 at 20:50

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.