locked
mock my interface RRS feed

  • Question

  • User1526025287 posted

    Hi all,


    i will have a interface class that has

    GetEmail()
    SendEmail()


    and for that interface class how do i write a mock classs and actual implementation to my interface and write a seperate class for unit test ?

    could some one clear this to me with some some code ? 

    actualluy i will be using different providers to  get and send emails ,  so looking at the right architecture to implement my code with mocks 



    thanks

    Monday, April 15, 2013 5:28 AM

Answers

  • User-1388383071 posted

    Creating your own mocking framework is very time consuming. but what the architeture you need is totaly depends on project

    But i like to suggest you some code depends on what i understod from your code. my understanding is listed here

    1) . you hava a .NET application which send mail to diffrent MailProviders also read mail .

    So we have to consider  adapter pattern, factory pattern in design patterns.

    public interface IEmailSender
        {
            void SendMail(); // Create diffrent Overloded function
        }
    
        // Factory Class
       public class MailSenderFactory
        {
            
           public static IEmailSender GetEmailSender(int id)
           {
    
               IEmailSender sender = null;
    
               switch (id)
               {
                       
                   case  1:
                       sender=new Gmail();
                       break;
                       
                   case 2:
                       sender=new Yahoo();
                       break;
                   case 3:
                       sender=new Hotmail();
                       break;
              
               }
               return sender;
    
           }
    
        }
    
        // Each concreate type
        public class Gmail : IEmailSender
        {
            public void SendMail()
            {
                // Write code for sending email using gmail
            }
        }
    
        public class Yahoo : IEmailSender
        {
            public void SendMail()
            {
                // Write code for sending email using gmail
            }
        }
    
        public class Hotmail : IEmailSender
        {
    
            public void SendMail()
            {
                //Write code for sending email using Hotail
            }
        }
    
    // Core implementations
    
    static void Main(string[] args)
            {
                // Send mail using gmail
                MailSenderFactory.GetEmailSender(1).SendMail();
                // yahoo
                MailSenderFactory.GetEmailSender(2).SendMail();
            }

    This is my code base from my undestading



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 15, 2013 11:55 AM

All replies

  • User-1388383071 posted

    The service of this  application is outside from your code ie the result of the send mail is depends on some external factors ,we are unable to say a mail send or not successfully. i dont think we can creat such external system setup using mocking. but the following  link may help you to know how to perfom mocking and unit testing

    http://pavelsem.blogspot.in/2009/04/mocking-in-c.html

    Monday, April 15, 2013 6:12 AM
  • User1526025287 posted

    i am not sure the example link u provided reflecting the scenario that i am looking for,

    basically my scenario that i need to implement is

    say for example i have couple of service providers to send emails on my applicaiton   :  gamil, yahoo, smtp etc

    so now i am planing to keepp  an interface class which holds generic methods like

    SendEmail()

    GetEmail()       etc

    and for that i am planing to create a different class libraries for each providers for gmail , yahoo smtp etc  ,

    so how do i implement with this scenario with some mock objects   ? i dont like to use any external tools for mocking my objects , just simple mock classes , and a simple unit test  to test my applicaiton .

    is there any one there to write a very simple code to reflect  this ?

    Monday, April 15, 2013 6:23 AM
  • User1526025287 posted

    no one answered to my question or suggested any architecture to build up this application ?

    Monday, April 15, 2013 10:36 AM
  • User-1388383071 posted

    Creating your own mocking framework is very time consuming. but what the architeture you need is totaly depends on project

    But i like to suggest you some code depends on what i understod from your code. my understanding is listed here

    1) . you hava a .NET application which send mail to diffrent MailProviders also read mail .

    So we have to consider  adapter pattern, factory pattern in design patterns.

    public interface IEmailSender
        {
            void SendMail(); // Create diffrent Overloded function
        }
    
        // Factory Class
       public class MailSenderFactory
        {
            
           public static IEmailSender GetEmailSender(int id)
           {
    
               IEmailSender sender = null;
    
               switch (id)
               {
                       
                   case  1:
                       sender=new Gmail();
                       break;
                       
                   case 2:
                       sender=new Yahoo();
                       break;
                   case 3:
                       sender=new Hotmail();
                       break;
              
               }
               return sender;
    
           }
    
        }
    
        // Each concreate type
        public class Gmail : IEmailSender
        {
            public void SendMail()
            {
                // Write code for sending email using gmail
            }
        }
    
        public class Yahoo : IEmailSender
        {
            public void SendMail()
            {
                // Write code for sending email using gmail
            }
        }
    
        public class Hotmail : IEmailSender
        {
    
            public void SendMail()
            {
                //Write code for sending email using Hotail
            }
        }
    
    // Core implementations
    
    static void Main(string[] args)
            {
                // Send mail using gmail
                MailSenderFactory.GetEmailSender(1).SendMail();
                // yahoo
                MailSenderFactory.GetEmailSender(2).SendMail();
            }

    This is my code base from my undestading



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 15, 2013 11:55 AM
  • Monday, April 15, 2013 12:00 PM
  • User1526025287 posted

    Hi binson143,

    really thanks for the sample code, like you said we will have to use a factory pattern when implementing this,

    for example if we are going to involve mock class  in a seperate assembly to write for unit test ? will this pattern chane in to some diiferent pattern ? 

    also how do write a mock class and test this using unit test ?  if some one can answer to this this will be a good example to everything  to understand it clearly .

    Monday, April 15, 2013 12:07 PM
  • User-1388383071 posted

    to test this it totaly depends on which value indicating the success. i can say if my code desnt return any exception  my code will work.

    So create a mock instance of IEmail and do the rest

    Monday, April 15, 2013 1:08 PM
  • User1526025287 posted

    Hi binson,

    never done the unit test through mock instance ? also dont know how to do this in coding .

    looking for some ones help in here. still trying to learn basics .

    Tuesday, April 16, 2013 4:00 AM