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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:29:19+00:00 2026-06-14T07:29:19+00:00

I have the following class that has two static methods Retrieve and RetrieveWithQuery. Below

  • 0

I have the following class that has two static methods Retrieve and RetrieveWithQuery.
Below the classes listed here, I have included a snippet of the test.
All but the last assert of the test fails with the following message:

Failed TestMethod2 MoqTest Assert.AreEqual failed. Expected:. Actual:<(null)>.

I understand that the problem may be that the query that I setup in the mock
is a different instance from the query used in the RetrieveWithQuery method.
And that is why is would be returning null.

In a perfect world I would simply re-factor the service class, unfortunately I am
working with legacy code that is already production. The goal is to first complete
tests, then re-factor code and run regression testing before updating production
environment.

Is there a workaround or different way to test this?

public class MyService  
{
    public virtual string RetrieveMethod(string account)
    {
        if (account == "The abc company")
        {
            return "Peter Smith";
        }

            return "John Doe";
    }

    public virtual string RetrieveMethod(MyQuery query)
    {
        return RetrieveMethod(query.QueryString);   
    }

    public static string Retrieve(MyService service, string value)
    {
        return service.RetrieveMethod(value);
    }

    public static string RetrieveWithQuery(MyService service, string value)
    {
        var query = new MyQuery
        {
            QueryString = value
        };

        return service.RetrieveMethod(query);
    }

}

public class MyQuery
{
    public string QueryString;
}



    [TestMethod]
    public void TestMethod2()
    {
        var mockService = new Mock<MyService>();

        const string company = "The abc company";
        const string expectedContact = "Peter Smith";

        var queryAccount = new MyQuery
                        {
                            QueryString = company
                        };

        // Setup base retrieve
        mockService.Setup(myServ => myServ.RetrieveMethod(company)).Returns(expectedContact);

        // Setup base retrieve with query
        mockService.Setup(myServ => myServ.RetrieveMethod(queryAccount)).Returns(expectedContact);

        // test base retrieve with query - PASS
        Assert.AreEqual(expectedContact, mockService.Object.RetrieveMethod(queryAccount));

        // test static method retrieve - PASS
        Assert.AreEqual(expectedContact, MyService.Retrieve(mockService.Object, company));

        // test static method retrieve with query - FAIL
        Assert.AreEqual(expectedContact, MyService.RetrieveWithQuery(mockService.Object, company));
    }
  • 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-14T07:29:20+00:00Added an answer on June 14, 2026 at 7:29 am

    Try this for your setup:

        // Setup base retrieve with query
        mockService.Setup(myServ => myServ.RetrieveMethod(It.Is<Query>(q=>q.QueryString == queryAccount.QueryString)).Returns(expectedContact);
    

    Or you could overload on Equals for Query so that the Query that gets created is equal to expectedQuery.

    The Moq QuickStart page has good examples of this and more which should help a lot.

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

Sidebar

Related Questions

I have a class that has the following property that is generated by the
So I have a class that has the following member variables. I have get
I have the following setup. Class, say, Car that has a CarPart (belongsTo=[car:Car]). When
I have the following methods in my Authentication class that is called by my
Assume that we have the following code: class Program { static volatile bool flag1;
I have the following code that is able to create a class that has
I have the following two classes: import java.io.*; import java.util.*; public class User {
I have a class that looks like the following: public class MyClass { private
I have the following class written by someone else that I'm trying to understand
We have a class a class that looks something like the following: public class

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.