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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:36:55+00:00 2026-06-13T09:36:55+00:00

I am pulling my hair out with this one. I have looked and cannot

  • 0

I am pulling my hair out with this one. I have looked and cannot find a simple, clear example of creating and using a partial stub with Microsoft Moles. Maybe I’m missing somethimg, or have my code architected poorly, but I can’t seem to get this to work.

Here’s my class (simplified):

public class AccountService : IAccountService {
        private readonly webServiceProxy IExternalWebServiceProxy;

    public AccountService(IExternalWebServiceProxy webServiceProxy) {
        this.webServiceProxy = webServiceProxy;
    }

    public List<AccountModel> GetAccounts(string customerId) {
         var returnList = new List<AccountModel>();
         var xmlResponse = webServiceProxy.GetAllCustomerAccounts(customerId);

         var accountNodes = xmlResponse.SelectNodes("//AccountNodes");
            if (accountNodes != null)
            {
                foreach (XmlNode node in accountNodes)
                {
                    var account = this.MapAccountFromXml(node);

                    if (!string.IsNullOrEmpty(account.AccountNumber))
                    {
                        returnList.Add(account);
                    }
                }
            }

            return returnList;
    }

    public AccountModel MapAccountFromXml(XmlNode node) {
        if (!IsValidAccount(node) {
            return null;
        }

        // This performs a lot of XML manipulation getting nodes based on attributes 
        // and mapping them to the various properties of the AccountModel. It's messy 
        // and I didn't want it inline with the other code.

        return populatedAccountModel;
    {

    public bool IsValidAccount(XmlNode node) 
    {
        var taxSelectValue = node.SelectSingleNode("//FORMAT/Field[@taxSelect='1']").First().Value;
        var accountStatus = // similar to first line in that it gets a single node using a specific XPath
        var maturityDate = // similar to first line in that it gets a single node using a specific XPath
        var maturityValue = // similar to first line in that it gets a single node using a specific XPath

        return taxSelectValue != string.Empty && taxSelectValue != "0" && (accountStatusValue != "CL" || (maturityDate.Year >= DateTime.Now.AddYears(-1).Year));
    }
}

What I want to do is test my GetAccounts() method. I can stub out the IExternalWebServiceProxy call and return fake XML, but I have internal calls happening in my service since my GetAccounts() method calls MapAccountFromXml() which in turn calls IsValidAccount().

Perhaps the solution is to not worry about breaking out the long and involved MapAccountFromXml() and IsValidAccount() code and just put them inline into the GetAccount() call, but I would rather leave them broken out for code readability.

I have my Moles assembly created, and know I can create a stub version of my class like this

var stubWebService = SIExternalWebServiceProxy {
       GetAllCustomerAccounts = delegate {
            return SomeHelper.GetFakeXmlDocument();
       }
}

var stubAccountService = new SAccountService() { callsBase = true; }

My problem is I don’t know how to then override the internal calls to MapAccountFromXml and IsValidAccount and I don’t want my Unit Test to be testing thos methods, I’d like to isolate GetAccounts for the test. I read somewhere the methods need to be virtual to be overriden in a partial stub, but could not find anything that then showed how to create a stub that overrides a few methods while calling the base for the one I want to test.

  • 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-13T09:36:56+00:00Added an answer on June 13, 2026 at 9:36 am

    Peer put me on the right track, thank you.

    It turned out that what I was looking for is called Detours in Moles. Rather than stub an interface using

    var stubAccountService = new SIAccountService();
    

    what I needed to do was create an instance of my AccountService and then detour all calls to the methods I wanted to mock, like this

    var accountService = new AccountService();
    
    MAccountService.AllInstances.MapAccountFromXmlXmlNode = delegate { 
        return new AccountModel(); 
    };
    

    The MAccountService is provided by Moles when you Mole your assembly. The only missing piece to this is that for this to work you need to add the following attribute to your test method:

    [HostType("Moles")]
    

    This worked for me locally, but in the end I had trouble getting TFS to do automated builds

    UPDATE

    I just stumbled on another way of doing this, while looking at Rhino Mocks. If the methods in the class being mocked are virtual then you can override them in the mock, like this:

    var accountService = new SAccountService();
    accountService.MapAccountFromXmlXmlNode = delegate
        {
            return new AccountModel();
        }
    

    Now I can call

    accountService.GetMemberAccounts();
    

    and when accountService makes its call to MapAccountFromXml it will be caught by the stub and processed as I deem necessary. No messing with HostType and it works like a charm.

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

Sidebar

Related Questions

Pulling my hair out over this one. I have one wordpress install at /2009
I am pulling my hair out on this one... I am using the ASIHTTPRequest
I'm pulling my hair out over this one - I have a page that
I am pulling my hair out on this one. I have a jquery ajax
I am pulling my hair out with this one. I have a table inside
I'm pulling my hair out over this one. I have an app that when
I am pulling my hair out over this one. I have a WCF interface
I've been pulling my hair out on this one all afternoon. Basically, I have
I have been literally pulling my hair out with this one and its beginning
I'm pulling my hair out on this one. I'm using the FlexSlider on a

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.