How do I make a mockup of System.Net.Mail MailMessage?

前端 未结 4 477
庸人自扰
庸人自扰 2021-02-04 02:47

So I have some SMTP stuff in my code and I am trying to unit test that method.

So I been trying to Mockup MailMessage but it never seems to work. I think none of the met

4条回答
  •  春和景丽
    2021-02-04 03:36

    In .NET 4.0 you can use the "duck-typing" to pass another class instead of "System.Net.Mail". But in earlier version, I'm afraid there is no another way, than create wrapper around "System.Net.Mail" nad the mock class.

    If is another (better) way, I would like to learn it :).

    EDIT:

    public interface IMailWrapper {
        /* members used from System.Net.Mail class */
    }
    
    public class MailWrapper {
        private System.Net.Mail original;
        public MailWrapper( System.Net.Mail original ) {
            this.original = original;
        }
    
        /* members used from System.Net.Mail class delegated to "original" */
    }
    
    public class MockMailWrapper {
       /* mocked members used from System.Net.Mail class */
    }
    
    
    void YourMethodUsingMail( IMailWrapper mail ) {
        /* do something */
    }
    

提交回复
热议问题