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

  • Home
  • SEARCH
  • 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 6023563
In Process

The Archive Base Latest Questions

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

I have SL code public void LoadData() { MyClient myClient = new MyClient(); myClient.MyMethodCompleted

  • 0

I have SL code

public void LoadData()
{
    MyClient myClient = new MyClient();
    myClient.MyMethodCompleted += new EventHandler<MyMethodCompletedEventArgs>(myMethod_MyMethodCompleted);
    myClient.MyMethodAsync();
}

then the completed method sets properties from what is returned from the server.

Works fine, but I want to unit test the class and the properties are all private.
I expected to do something along the lines of

public void LoadData(IMyClient myClient = null)
{
    if(myClient == null) {
        MyClient myClient = new MyClient();
    }
    ...

and pass in a mock, but the interface for the client does not contain the async methods but only the original server side non async methods
i.e. public ReturnType MyMethod();

So I tried using MyClient as a base class of a Mock object

public MyMockClient : MyClient
new public ReturnType MyMethod() 
{
     ...

but the base constructor causes all sorts of problems and I do not want to add another service reference to the test project.

Am I missing a trick, can this be done without any third party mocking tools ?

  • 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-23T04:01:27+00:00Added an answer on May 23, 2026 at 4:01 am

    I would suggest that you specify the MyClient instance in the constructor of your class:

    public class MyClass
    {
        private MyClient _client;
    
        public MyClass()
            : this(new MyClient())
        { }
    
        public MyClass(MyClient client)
        {
            _client = client;
            _client.MyMethodCompleted += ...
        }
    
        public void LoadData
        {
            _client.MyMethodAsync();
        }
    }
    

    Then you can either subclass MyClient (or better yet, refactor it to implement an interface) to implement your mock version of the client.

    In response to comments
    In order to implement an interface on the auto-generated WCF client class, you can use Partial Classes. For example, assuming your auto-generated client is called MyClient in the namespace MyNamespace, your partial class would need to look like the below:

    namespace MyNamespace
    {
        public partial class MyClient : IMyClientInterface
        {
    
        }
    }
    

    Assuming that the generated MyClient contains all of the members on IMyClientInterface then no further code is needed and you can construct your class on an instance of IMyClientInterface

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

Sidebar

Related Questions

I have the following code: public class Test { public static void Main() {
I have the following code: public void SetMove(Position3D pos, float time, float linearity, bool
I have this code: public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED) { int
I have the code : public void AlarmStart() { Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE,
I have the following code: public void onSurfaceCreated(GL10 gl, EGLConfig config) { gl.glClearColor(0.0f, 0.0f,
I have this code: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView lv =
I have this code: public void AddNode(string Node) { try { treeView.Nodes.Add(Node); treeView.Refresh(); }
I have this code : public void GenerateWtW() { ExecutorService exec = Executors.newFixedThreadPool(30); ConcurrentHashMap<String,
Okay, so, here's what I have in code: public void makeObject(int i){ String s
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public

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.