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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:21:10+00:00 2026-06-05T06:21:10+00:00

With Moq, is it possible to setup a mocked property so that it mutates?

  • 0

With Moq, is it possible to setup a mocked property so that it mutates?

For example, given:

public interface IService // To be mocked.
{
    bool IsConnected { get; }
}

Is it possible to setup a mock of IService such that the following passes?

var mockService = new Mock<IService>();

// Setup mockService so IsConnected mutates...?

bool before = mockService.Object.IsConnected;

bool after = mockService.Object.IsConnected;

Assert.AreNotEqual(before, after);

Certainly this setup doesn’t do the trick:

mockService.SetupGet(service => service.IsConnected).Returns(false);
mockService.SetupGet(service => service.IsConnected).Returns(true);

That is consistent my understanding that Moq does not implement the record/playback model.

Here is a little more realistic example:

public interface IService // To be mocked.
{
    bool IsConnected { get; }

    void Connect(); // Mutates IsConnected.
}

class Client // To be tested
{
    private readonly IService service;

    public Client(IService service)
    {
        this.service = service;
    }

    public bool DoWork()
    {
        if (!service.IsConnected)  // In test, IsConnected should return false.
        {
            service.Connect();
        }

        if (!service.IsConnected) // In test, IsConnected should return true.
        {
            return false;
        }

        // ... yada yada yada more code

        return true;
    }
}

I’m using Moq 4.0.10827.0.

  • 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-05T06:21:12+00:00Added an answer on June 5, 2026 at 6:21 am

    You can set Returns to a Func<bool> instead of a bool. You would then have write that func such that it changes its result. Something like….

    bool isConnected = false
    var mockService = new Mock<IService>();
    // Setup mockService so IsConnected mutates...?
    mockService.Setup(s => s.Connect()).Callback(() => isConnected = true);
    mockService.SetupGet(s => s.IsConnected).Returns(() => isConnected);
    
    bool before = mockService.Object.IsConnected;
    
    mockService.Object.Connect();
    
    bool after = mockService.Object.IsConnected;
    
    Assert.AreNotEqual(before, after);
    

    However, depending on how complicated your serivce gets, you may want to consider making a FakeService and use that instead of mocking.

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

Sidebar

Related Questions

Is it possible to use Moq to mock an object that implements an interface
Possible Duplicate: How to mock a method that returns an int with MOQ Here's
I am learning Moq, and I would like to mock an interface ISecureAsset that
is it legal to MOQ a class that does not inherit an interface like
I want to mock this interface using Moq IInterfaceToBeMocked { IEnumerable<Dude> SearchDudeByFilter(Expression<Func<Dude,bool>> filter); }
Is it possible to use Moq to say a method accepts a string that
I want to add MOQ .net library to a .NET 4.0 project. I get
I got a problem with Moq and Automapper regarding IDataReader. I found an example
This is probably a noob question that I will get slated for but here
I am creating a mock for my ITransformer interface. public interface ITransformer { String

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.