Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say I have the following method that sends emails. I know I should have tests for the different status codes returned. I am also testing if the the methods CreateMail() and Send() are being called.

I guess in the end what's important is if the success status is returned or not. Should I be testing if these methods are being called or not ?

public void SendMail(Model model, out Status status)
    if (!MyValidator.IsValidEmailAddress(model.EmailRecipient))
        status = Status.InvalidEmailAddress;

    // code removed for clarity

    IEmailMessage email = _emailMessageBuilder.CreateMail(model);


    status = Status.Success;

public void SendMail_CreateMailGetsCalledOnce()
{ ... }

public void SendMail_SendGetsCalledOnce()
{ ... }
share|improve this question

migrated from Aug 22 '14 at 11:16

This question came from our site for peer programmer code reviews.

Please explain why the downvote ? – ThunderDev Aug 22 '14 at 9:30
This question appears to be off-topic because it is about unit testing best practices, and not aksing for a code review. It might be on topic on Programmers. For more details on what is acceptable here, please go to the help center. – amon Aug 22 '14 at 10:39
up vote 5 down vote accepted

You should avoid doing assertions on the method's innards as much as possible, to avoid coupling the unit tests to implementation specific details. In reality, of course, that's not always possible.

In this case, you don't need to verify whether those methods were called.

You're gonna have to provide a mock for _emailMessageBuilder, IEmailMessage and _mailerClient. If:

  • the mailer client is setup to return a status code of 200 if it is asked to send the email created by the message builder,
  • and if the message build is setup to return that email if it is asked to create an email for model,
  • and if SendMail returns 200,

then you can infer that the right methods were called. No need to explicitly verify this.

share|improve this answer
Thanks, I also found a nice blog post explaining interaction testing – ThunderDev Aug 22 '14 at 12:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.