tag:blogger.com,1999:blog-8412831559140848595.post1386949024286999465..comments2022-12-13T17:45:05.444+02:00Comments on Helper Code: How to write a unit testAnonymoushttp://www.blogger.com/profile/04664241287712801778noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-8412831559140848595.post-692123183805996302013-05-18T17:54:50.070+03:002013-05-18T17:54:50.070+03:00Actually you don't TRULY need to abstract Smtp...Actually you don't TRULY need to abstract SmtpClient. http://mikehadlow.blogspot.com/2010/01/configuring-smtpclient-to-drop-emails.html you can use configuration to make SmtpClient write to disk instead of actual SMTP. This might not cover every scenario, but it is generally sufficient. After I've started using SpecifiedPickupDirectory I've never again abstracted SmtpClientdotnetchrishttp://www.Marisic.Net/noreply@blogger.comtag:blogger.com,1999:blog-8412831559140848595.post-88873871609581663802013-05-18T16:57:15.179+03:002013-05-18T16:57:15.179+03:00I get that. I suppose my real question was about ...I get that. I suppose my real question was about just putting a facade over SmtpClient. As far as I recall you'd have to put a facade over it anyway to get virtual methods and not actually send an email when testing.nportellihttp://muddypa.wsnoreply@blogger.comtag:blogger.com,1999:blog-8412831559140848595.post-44721342616607649312013-05-18T01:39:35.700+03:002013-05-18T01:39:35.700+03:00You test using the same mocking tools. You can rew...You test using the same mocking tools. You can rewrite virtual methods on objects with any good mocking tool. My personal favorite combination is MSpec+Machine.Fakes it makes it like ice cream at how smooth it becomes to mock things.<br /><br />The next major reason using concrete objects with virtual methods is substantially better than IEmailClient. It simplifies dependency injection. With a concrete object, you will not have to tell any decent IOC container any single thing about your object. It will just use constructor injection as needed because it knows it needs to do new MyEmailClient(), it doesn't need a mapping of IEmailClient to EmailClient (even though lots of containers can guess these things for you)<br /><br />One less code file to maintain. Add in having xml documentation on methods, it now has 2 places it needs to be maintained also.<br /><br />The last simple reason is that it's more honest. As I talked about IEmailClient isn't being used to define a contract used by many services. By using a concrete class you acknowledge there's a dependency here and the standard implementation is directly in line without impacting testing negatively.<br /><br /><br />I was guilty of this myself for years, only in the past year have i really come to understand this.dotnetchrishttp://www.Marisic.Net/noreply@blogger.comtag:blogger.com,1999:blog-8412831559140848595.post-1543332723231742192013-05-16T18:10:42.256+03:002013-05-16T18:10:42.256+03:00So how would you test if the email was sent if you...So how would you test if the email was sent if you didn't use an interface? Why is marking a method as virtual better than using interfaces?nportellihttp://muddypa.wsnoreply@blogger.com