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

  • Home
  • SEARCH
  • 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 888523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:24:44+00:00 2026-05-15T13:24:44+00:00

Using Moq I am mocking a property, Report TheReport { get; set; } on

  • 0

Using Moq I am mocking a property, Report TheReport { get; set; } on an interface ISessionData so that I can inspect the value that gets set on this property.

To achieve this I’m using SetupGet and SetupSet as follows:

// class-level fields
protected Report _sessionReport;
protected Mock<ISessionData> SessionData { get; private set; }

And in my setup method…

SessionData = new Mock<ISessionData>();

SessionData
    .SetupSet(s => s.TheReport = It.IsAny<Report>())
    .Callback<RDLDesigner.Common.Report>(r =>
    {
        _sessionReport = r;
        SessionData.SetupGet(s => s.TheReport).Returns(_sessionReport);
    });

I found this approach on StackOverflow and it works, but I do not understand why. I expected to have the call to SetupGet outside of the SetupSet callback.

Can anyone explain how and why this approach works, and if it is the most appropriate way of mocking a property of this type?

Edit

Using SessionData.SetupProperty(s => s.TheReport); also works in my scenario, but I am still interested in any explanations for how and why my original approach worked.

  • 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-15T13:24:44+00:00Added an answer on May 15, 2026 at 1:24 pm

    The reason why the callback is used in the call to SetupGet is that the _sessionReport reference is passed by value, this means that a subsequent call to the Set method would not update the value returned by the get method.

    To see what’s going on more clearly. If you had setup your Mock as follows:-

    SessionData.SetupSet(s => s.Report = It.IsAny<Report>());
    SessionData.SetupGet(s => s.Report).Returns(_report);
    

    In pseudocode the Mocked implementation will look a little like

    public Report Report {
        set { }
        get { 
           // Copy of the value of the _report reference field in your test class
           return _reportCopy; 
        }  
    }
    

    So doing something like this wouldn’t work:-

     ISessionData session = SessionData.Object
     Report report = new Report();
     session.Report = report;
     session.Report.ShouldEqual(report); //Fails
     _report.ShouldEqual(report); // Fails
    

    Obviously we need to add some behaviour to the Set method so we set up the Mock like so

    SessionData.SetupSet(s => s.Report = It.IsAny<Report>())
               .Callback(s => _report = s);
    SessionData.SetupGet(s => s.Report).Returns(_report);
    

    This leads to the Mocked implementation looking a little like

    public Report Report {
        set {
           // Invokes delegate that sets the field on test class
        }
        get { 
           // Copy of the original value of the _report reference field
           // in your test class
           return _reportCopy; 
        }  
    }
    

    However this leads to the following problem:-

      ISessionData session = SessionData.Object
      Report report = new Report();
      session.Report = report;
      _report.ShouldEqual(report); // Passes
      session.Report.ShouldEqual(report); //Fails!
    

    In essence the “get” method on the property still returns a reference to the original object _report was pointing to as the reference was passed by value to the SetupGet method.

    We therefore need to update the value the report getter returns every time the setter is called which leads to your original code

    SessionData
       .SetupSet(s => s.TheReport = It.IsAny<Report>())
       .Callback<RDLDesigner.Common.Report>(r => {
            _sessionReport = r;
            SessionData.SetupGet(s => s.TheReport).Returns(_sessionReport);
       });
    

    This ensures that the value returned by the Get method is always kept in sync with the previous call to the set method. And leads to something that (functionally) behaves like:-

    public Report Report {
        set {
           // Sets the field on the test class
           _reportCopy = value;
        }
        get { 
           // Copy of the value of the _report reference field in your test class
           return _reportCopy; 
        }  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.