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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:08:34+00:00 2026-06-07T16:08:34+00:00

I am using Moq library for unit testing. Now what i want is that

  • 0

I am using Moq library for unit testing. Now what i want is that when I access my object for the first time it should return null, and when i access this on second time it should return something else.

here is my code

var mock = new Mock<IMyClass>();
mock.Setup(?????);
mock.Setup(?????);

var actual = target.Method(mock.object);

in my method i am first checking that whether mock object is null or not, if it is null then do initialize it and then do some calls on it.

bool Method(IMyClass myObj)
{
    if (myObj != null)
        return true;
    else
    {
        myObj = new MyClass();
        bool result = myObj.SomeFunctionReturningBool();
        return result;
    }
}

what to do setup for mock object,

Also i need to know how to mock this line

bool result = myObj.SomeFunctionReturningBool();
  • 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-07T16:08:36+00:00Added an answer on June 7, 2026 at 4:08 pm

    It sounds like you are trying to run two tests with one test method – maybe it would be better to split the tests into two?

    You also want to initialise a new object if the method is passed null. To test this, I suggest creating a factory object responsible for creating instances of MyClass. The new code would look like:

    interface IMyClassFactory
    {
        IMyClass CreateMyClass();
    }
    
    bool Method(IMyClass myObj, IMyClassFactory myClassFactory)
    {
        if (myObj != null)
        {
            return true;
        }
    
        myObj = myClassFactory.CreateMyClass();
        return myObj.SomeFunctionReturningBool();
    }
    

    Then the tests would look like:

    [Test]
    public void Method_ShouldReturnTrueIfNotPassedNull()
    {
        Assert.That(target.Method(new MyClass()), Is.True);
    }
    
    [Test]
    public void Method_ShouldCreateObjectAndReturnResultOfSomeFunctionIfPassedNull()
    {
        // Arrange
        bool expectedResult = false;
    
        var mockMyClass = new Mock<IMyClass>();
        mockMyClass.Setup(x => x.SomeFunctionReturningBool()).Returns(expectedResult);
    
        var mockMyFactory = new Mock<IMyClassFactory>();
        mockMyFactory.Setup(x => x.CreateMyClass()).Returns(mockMyClass.Object);
    
        // Act
        var result = target.Method(null, mockMyFactory.Object);
    
        // Assert
        mockMyClass.Verify(x => x.SomeFunctionReturningBool(), Times.Once());
        mockMyFactory.Verify(x => x.CreateMyClass(), Times.Once());
        Assert.That(result, Is.EqualTo(expectedResult));
    }
    

    Here the factory pattern has been used to pass in an object which can create objects of IMyClass type, and then the factory itself has been mocked.

    If you do not want to change your method’s signature, then create the factory in the class’s constructor, and make it accessible via a public property of the class. It can then be overwritten in the test by the mock factory. This is called dependency injection.

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

Sidebar

Related Questions

I'm doing some unit testing, and mocking some properties using Moq . Now, this
I am using the Moq framework for unit testing and would like to be
I am using Moq for unit testing, and I am trying to write my
I'm using Moq library. I'm mocking up an instance that does all regular CRUD
So, I'm using moq for testing, but I ran into a problem that prevents
I'm having a bit of trouble doing some unit-testing using moq. If I have
I am using Moq for unit testing and I would like to test for
I'm mocking the WebOperationContext class over wrapper for unit testing (using Moq). But I
For unit testing, I'm using NUnit 2.6 and Moq 4.0. There's a particular case
Using Moq for generation of Stubs and Mocks in my unit tests, I have

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.