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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:39:14+00:00 2026-05-23T11:39:14+00:00

I am trying to get to grips with Moq and using a simple example

  • 0

I am trying to get to grips with Moq and using a simple example to figure it out. I am using Google to geocode an address. I have wrapped WebClient so it can be mocked. Here is the code:

public class Position
{
    public Position(double latitude, double longitude)
    {
        Latitude = latitude;
        Longitude = longitude;
    }
    public virtual double Latitude { get; private set; }
    public virtual double Longitude { get; private set; }
}

public interface IWebDownloader
{
    string Download(string address);
}

public class WebDownloader : IWebDownloader
{
    public WebDownloader()
    {
        WebProxy wp = new WebProxy("proxy", 8080);
        wp.Credentials = new NetworkCredential("user", "password", "domain");

        _webClient = new WebClient();
        _webClient.Proxy = wp;
    }

    private WebClient _webClient = null;

    #region IWebDownloader Members

    public string Download(string address)
    {
        return Encoding.ASCII.GetString(_webClient.DownloadData(address));
    }

    #endregion
}

public class Geocoder
{
    public Position GetPosition(string address, IWebDownloader downloader)
    {
        string url = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false",
                                   address);
        string xml = downloader.Download(url);

        XDocument doc = XDocument.Parse(xml);

        var position = from p in doc.Descendants("location")
                       select new Position(
                           double.Parse(p.Element("lat").Value),
                           double.Parse(p.Element("lng").Value)
                           );

        return position.First();
    }
}

All good so far. Now here is the unit test with Moq:

    [TestMethod()]
    public void GetPositionTest()
    {
        Mock<IWebDownloader> mockDownloader = new Mock<IWebDownloader>(MockBehavior.Strict);
        const string address = "Brisbane, Australia";
        mockDownloader.Setup(w => w.Download(address)).Returns(Resource1.addressXml);

        IWebDownloader mockObject = mockDownloader.Object;

        Geocoder geocoder = new Geocoder();
        Position position = geocoder.GetPosition(address, mockObject);

        Assert.AreEqual(position.Latitude , -27.3611890);
        Assert.AreEqual(position.Longitude, 152.9831570);
    }

The return value is in a resource file and is the XML output from Google. Now when I run the unit test I get the exception:

All invocations on the mock must have a corresponding setup..

If I turn off strict mode, then the mock object returns null. If I change the setup to:

mockDownloader.Setup(w => w.Download(It.IsAny<string>())).Returns(Resource1.addressXml);

then the test runs fine. But I don’t want to test for any string, I want to test for this specific address.

Please put me out of my misery and tell my where I am going wrong.

  • 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-23T11:39:14+00:00Added an answer on May 23, 2026 at 11:39 am

    As far as I can tell, you’re having your mock return a specific value when it receives the string “Brisbane, Australia”, but you’re passing it the value http://maps.googleapis.com/maps/api/geocode/xml?address=Brisbane,%20Australia&sensor=false (or however it ends up getting formatted).

    Try something like this in your test code:

    …
    const string address = "Brisbane, Australia";
    const string url = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false", address);
    mockDownloader.Setup(w => w.Download(url)).Returns(Resource1.addressXml);
    …
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get to grips with Qt's signal and slots mechanism. I have
I'm trying to get to grips with regular expressions: I have a database connection
I am trying to get to grips with programmatically configuring views. I have a
I am trying to get to grips with BDD web development in Ruby using
I'm trying to get to grips with Cocos2d by trying to accomplish simple things.
I have been trying to get to grips with Hibernate's inverse attribute, and it
I'm trying to get to grips with MSTest. I'm using VS2010 with entity framework
I am trying to get to grips with MS Dynamics CRM 2011. I have
I am trying to get to grips with EF4 CTP5. I have two classes
I'm still trying to get to grips with regexps and I'm considering a simple

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.