Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6005915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:27:55+00:00 2026-05-23T01:27:55+00:00

I have a class that reads a pop3 email server. I want to write

  • 0

I have a class that reads a pop3 email server. I want to write some unit tests for the containing class, so I need to mock the POP3 server classes. I am using OpenPOP.Net. So I have a constructor like this:

EmailHost(string email, string password, string pop3Address, int port)
... etc....

Then later I have a connect class:

private void Connect()
{
    _pop3 = new Pop3Client();
    etc...

So the reference to the Pop client is buried in the guts of the class. To be able to mock it I need to do some funky stuff, since as making EmailHost a generic on the Pop3Client, or adding some function with a default to do the above line:

 public EmailHost<PopClass>(...)

or

public EmailHost(... params, Func popGenerate = () => new Pop3Client())

So it seems I have to compromise my constructor a little to make it work with mock. (Obviously I’d define an IPop3Client or so forth.)

Any thoughts on how to do this correctly?

EDIT:

jOk provides an interesting answer, problem is that it doesn’t fit well within the structure of what I am doing. In this particular case I need to handle multiple email addresses in the Pop client, and the are added dynamically. Consequently, as you add each email address, you need to create a new Pop3Client. Additionally, I want to hide the implementation of the POP3 itself, so that they user doesn’t need to know about it as much as possible.

Any other thoughts on this?

Thanks for you input btw.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:27:56+00:00Added an answer on May 23, 2026 at 1:27 am

    I think you’re close.

    Instead of passing in all those arguments that are eventually going to be used to create an instance of Pop3Client, consider injecting an instance of Pop3Client.

    public EmailHost(IPop3Client client) {
    
    }
    

    By doing this, you’re removing your EmailHost class from the responsibility of creating the Pop3Client object. This responsibility is instead shifted somewhere else, most commonly to an IoC container.

    I can’t tell from the code you posted whether Pop3Client is based on an interface. If it is, great, you can just mock/stub it in your test and inject the dependency. If not, then there’s more work involved in manipulating it so that it’ll work in this scenario. It can be as simple as writing a wrapper (and corresponding interface) around the class that exposes the methods that you’re using inside of EmailHost. This can then be used in place of IPop3Client in the example above.

    Does that make sense? Let me know if you need more guidance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class, say, CDownloader, that reads some XML data and provides access
I have a class that outputs a simple report file. It reads some record
Problem in words: For my application, I have a class that reads from a
I have a multi-R/W lock class that keeps the read, write and pending read
I have a class that I want to use to store properties for another
I have a csv reader class that reads a .csv file and its values....
I have a class that reads the state of what string is in a
I have written a class(es) that writes and reads from hdfs. Given certain conditions
I have a simple class that defines some constants, e.g.: module Foo class Bar
I'm trying to write a class that reads HTTP requests and responses and parses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.