How do you unit test a mail sending function

后端 未结 6 2106
礼貌的吻别
礼貌的吻别 2021-01-31 17:41

I have a function which creates a report and sends it to a user. What I\'ve done is create a mock for the email function, and verify if the \'send\' function of the email class

6条回答
  •  清酒与你
    2021-01-31 18:41

    At least in my case, all email sending is encapsulated in a 'MailService' class - and all send calls go through a send method in the class. The send method is virtual - and all it does is new up an SmtpClient, send the mail, etc. For my unit tests, I have a private class MailServiceMock, that overrides send and does nothing. All of the other methods can be tested - but send is short-circuited.

    oleschri has provided an easier way to do this using Rhino Mocks (I think any mocking framework would work fine) - however if you are hesitant to introduce another dependency, this can get your feet wet with mocking by hand.

提交回复
热议问题