Take the 2-minute tour ×
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.

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;
        return;
    }

    // code removed for clarity

    IEmailMessage email = _emailMessageBuilder.CreateMail(model);

    _mailerClient.Send(email);

    status = Status.Success;
}

[TestMethod]
public void SendMail_CreateMailGetsCalledOnce()
{ ... }

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

migrated from codereview.stackexchange.com Aug 22 at 11:16

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

    
Please explain why the downvote ? –  ThunderDev Aug 22 at 9:30
1  
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 at 10:39

1 Answer 1

up vote 4 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 at 12:45

Your Answer

 
discard

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.