Jonathan
Jonathan

Reputation: 7098

TDD In practice -- How do you write a unit test for sending an email?

Client says he wants a button that when pushed, takes the form below and sends it as an email. How would you unit test this?

Assume you are:

From what I understand, you could use a UI mocker to click the button, then wait a minute or so, then use IMAP to count if there is one more message received than before. But this sounds like it violates TDD goals in almost every category -- it's not fast, it's not atomic, it's complex, it requires it's own IMAP dependency. What's the "TDD" way to do this?

Upvotes: 2

Views: 421

Answers (1)

Paul
Paul

Reputation: 1875

What part of YOUR program do you want to test?

Please note what I highlighted there - you want to test your code.

You do not test things out of your control unless you absolutely need to. So, I would say, if you use SMTP/IMAP libraries - you trust that they do what they are supposed to, you don't write tests to verify that, similarly as you don't check the File.write() actually writes to file, etc.

Back to your question, please look into creating and adapter/simulator for email behavior. Use in-memory implementation in your test, and use the real thing in your production code.

Eric Gunnerson has written a good post about them as well as published a kata to github

Upvotes: 2

Related Questions