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 7740301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:44:33+00:00 2026-06-01T08:44:33+00:00

Background: I have spun up a simple Proxy thing for my Wcf client based

  • 0

Background:

I have spun up a simple Proxy thing for my Wcf client based partly on examples found online and partly on my needs.

The usage for creating a client proxy as its known is:

WcfClientProxy<IServicecontract> clientProxy = 
                                 new WcfClientProxy<IServiceContract>();

WcfClientProxy has a method called Execute which takes : Expression<Func<TChannel, TResult>> or Expression<Action<TChannel>>.

What I’m trying to achieve:

I am trying to mock this using Moq so I can test the calls made to the service contract via the client.

So mock a call like this..

clientProxy.Execute(m=>m.DoSomeAction(5));

The problem:

The problem is that the mock does not work. I get this error:

” threw exception:
System.NullReferenceException: Object reference not set to an instance of an object.”

My test service contract is:

public interface ITestingServiceInterface : System.ServiceModel.IClientChannel
{
    string Version();
    VersionDetail VersionDetail();
    IList<VersionDetail> VersionDetails();
    void DoSomeDelete(int itemId);
}

The wcf client proxy interface:

public interface IWcfClientProxy<TChannel> where TChannel : ICommunicationObject
{
    bool ThrowOnException { get; set; }
    TResult Execute<TResult>(Expression<Func<TChannel, TResult>> operation);
    void Execute(Expression<Action<TChannel>> expression);
}

My attempted test and mock with setup..

private List<ProdItem> items;
private Mock<IWcfClientProxy<ITestingServiceInterface>> mockClientProxy;

[TestInitialize]
public void SettingUp()
{
    mockClientProxy = new Mock<IWcfClientProxy<ITestingServiceInterface>>();

    items = new List<ProdItem>();
    for( int i =0; i<10; i++){
        items.Add(new ProdItem { ProdItemId = i, LocalStock = i });
    }
}

[TestMethod]
public void SimpleTest()
{
    mockClientProxy.Setup(m => m.Execute(x => x.DoSomeDelete(It.IsAny<int>()))).Callback(RemoveItem);

    var client = mockClientProxy.Object;

    client.Execute(x => x.DoSomeDelete(4));

    Assert.AreEqual(9, items.Count);
}

public void RemoveItem()
{
    items.RemoveAt(items.Count - 1);
}

The error for this test is actually that the assert is wrong, it seems like the Callback is never being hit.

I’m sure I have just done something stupid.

Update:
Showing an example of what I would like to be able to test if the above worked..

[TestMethod]
public void SimpleTest()
{
    mockClientProxy.Setup(m => m.Execute(x => x.DoSomeDelete(It.IsAny<int>()))).Callback(RemoveItem);

    var client = mockClientProxy.Object;

    var stockHelper = new StockHelper(client);

    stockHelper.DeleteItem(5);

    Assert.AreEqual(9, items.Count);
}

In the above I am testing the StockHelper Class that take a client proxy in its constructor through which it does its calls to WCF.

To test the StockHelper (or what ever) I need to be able to mock the client and its service calls. The above (1st) example demon-straights me having an issue trying to do this.

I hope that makes sense, please see that my end goal is not to test a mock.. that is just where I have got to trying to debug my issue.

  • 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-06-01T08:44:34+00:00Added an answer on June 1, 2026 at 8:44 am

    As I can see from your code, you are testing mock. I don’t see any real object you are testing. Mocks are used to stub dependencies of objects being tested. So, if some class Foo uses IWcfClientProxy then you provide mock of this proxy to class Foo. And verify that during Foo.Bar() execution method DoSomeDelete of dependency was called. That’s the purpose of mocks.

    [TestClass]
    public class FooTests
    {
       private Foo _foo;
       private Mock<IWcfClientProxy<ITestingServiceInterface>> _clientProxy;
    
       [TestInitialize]
       public void SettingUp()
       {
           _clientProxy = new Mock<IWcfClientProxy<ITestingServiceInterface>>();
           _foo = new Foo(_clientProxy.Object);
       }
    
       [TestMethod]
       public void SimpleTest()
       {
           // Act on object being tested
           _foo.Bar(5);
    
           // verify it executed correct method on dependency
           _clientProxy.Verify(cp => cp.Execute(x => x.DoSomeDelete(5)));
        }
    }
    

    Keep in mind, when testing Foo you should not care how _clientProxy is implemented.

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

Sidebar

Related Questions

Background I have an existing extension designed to accompany a browser-based game (The extension
Background: I have a WCF service deployed on my local machine that in turns
Background I have been asked by a client to create a picture of the
Background : I have created a web service client VoucherWebService (consumed from an external
Background I have a dimension table that has a single record for each day.
Background: I have this with rollup query defined in MySQL: SELECT case TRIM(company) when
Background: We have a TFS server setup where me manage our source code and
Background: I have a ListView / GridView with several columns. In some situations, only
Background I have a custom collection that is binded to the datagridview this.datagridview.DataSource =
Background: We have two directories of Crystal Reports for both the web side of

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.