Sergey Aleynik
Sergey Aleynik

Reputation: 43

Test Driven Development and KISS

For example I want to design a Job entity. Job has a status and I need to mark a Job as done.

I wrote a markDone() method and I want to test it. So to do an assertion I need one more method - isDone(). But at the moment I don't use isDone() method in the my code.

Is it ok to write such a useless methods to please a TDD?

Upvotes: 0

Views: 168

Answers (3)

VoiceOfUnreason
VoiceOfUnreason

Reputation: 57327

Is it ok to write such a useless methods to please a TDD?

Yes, but maybe not.

The yes part: you are allowed to shape your design in such a way that testing is easier, including creating methods specifically for observability. Such things often come in handy later when you are trying to build views to understand processes running in production.

The maybe not part: What's Job::status for? What observable behavior(s) in the system change when that status is set to done? What bugs would be filed if Job::markDone were a no-op? That's the kind of thing you really want to be testing.

For instance, it might be that you need to be able to describe the job as a JSON document, and changing the job status changes the value that appears in the JSON. Great! Test that.

job.markDone()
json = job.asJson()
assert "DONE".equals(json.getText("status))

In the domain-layer / business-logic, objects are interesting for how they use their hidden data structures to respond to queries. Commands are interesting, not because they mutate the data structure, but because the mutated data structure produces different query results.

Upvotes: 1

Vin Shahrdar
Vin Shahrdar

Reputation: 1231

Can `isDone()' just reside in the test project and not the production code? That way, you wouldn't have useless production code just for the sake of testing.

Upvotes: 0

izeke
izeke

Reputation: 75

If the method has no use outside of testing, I'd avoid writing it since there's probably another way that you can check whether or not the job is done. Is the job's status public, or do you have a method that returns the job's status? If so, I'd use that to test whether markDone() is working properly. If not, you can use reflection to check the value of a private or protected object property.

Upvotes: 0

Related Questions