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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:26:30+00:00 2026-06-15T17:26:30+00:00

I’ve asked a question before on how to set value of interface in testing

  • 0

I’ve asked a question before on how to set value of interface in testing a method
. I’ve successfully implemented the Moq framework into my project and the test runs fine.

this is the sample code that I’ve featured:

public void PostEvent(
            eVtCompId inSenderComponentId, 
            eVtEvtId inEventId, 
            long inEventReference, 
            IF_SerializableData inEventData)
{
    if(mEventMap.ContainsKey(inEventId))
    {
        mEventMap[inEventId](inSenderComponentId, inEventReference, inEventData);
    }
}

Here I have 4 parameters: 1st: an enum, 2nd: another enum, 3rd: long, 4th: an interface. However, I was mistaken in that the 4th parameter (the interface), isn’t supposed to be an interface, but rather a reference to the interface.

so it should look like this:

public void PostEvent(
       eVtCompId inSenderComponentId, 
       eVtEvtId inEventId, 
       long inEventReference, 
       ref IF_SerializableData inEventData)

the sample Moq test code that was given to me (which is this)…

var serializable = new Mock<IF_SerializableData>();
target.PostEvent(..., serializable.Object);

…doesn’t work. I’ve tried ref serializable.Object but it still doesn’t work because I get an error that says the ref parameter is expecting a reference to a variable, not an object.

Any tips or examples on how to properly test this?

  • 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-15T17:26:31+00:00Added an answer on June 15, 2026 at 5:26 pm

    You need to copy the Object reference from the serializable mock into a local variable so you can then pass it as ref.

    IF_SerializableData localRef = serializable.Object;
    target.PostEvent(..., ref localRef);
    

    You can’t pass ref Serializable.Object because its a property – see also Is it possible to pass properties as "out" or "ref" parameters? which offers an excellent discussion of why this is the case, and a source of other links.

    My explanation

    This is ultimately because properties are not variables. A read/write property is a pair get and set accessor method(s) providing variable-like capabilities but, crucially, when you get a property you always get a copy of the underlying variable – even if that variable has a reference type.

    So:

    public class MyClass {
      private object _property;
      public object Property {
        get { return _property; } //<-- _property reference copied on to stack & returned
                                  //    here as a new variable.  Therefore a ref
                                  //    on that is effectively meaningless.
                                  //    for a ref to be possible you'd need a pointer 
                                  //    to the _property variable (in C++ terms)
        set { _property = value; }
      }
    }
    

    In this example – if you could pass ref MyClass.Property to a method – it would be meaningless because it would be passing a reference to a transient variable on the stack – i.e. the copy of the reference returned by the Property get accessor; it would not be passing the property by reference. So C# doesn't allow it, even though it could, because it would imply thatProperty` can be modified by the method – when it simply can’t.

    Hence why we need to capture that value from the stack and copy it into a local variable in your case. Now – note that in order for the new value set by the ref method to appear on your Mock<T> you’d need to set it’s Object property to the local variable value again (if you can – I don’t use Moq but I assume it’s Mocks are immutable).

    A lot of debate has been had as to whether C# should automatically handle ref Property in this way (see the aforementioned SO I linked to). To my mind it’s similar to ref Derived not being compatible with ref Base – yes there is a way that the language can handle this automatically for you, but should it? In my mind, no. Do I get frustrated by it? Oh yes of course – but often I find it highlights architectural weaknesses which really should be fixed (for example, relying on ref or out parameters where a return value is likely better).

    The only way for C# to allow you to pass a property by reference would be to pass the get and set accessors to the target method – and this would not be compatible with ref at all (because that’s just a memory location).

    As a taster you’d have to write such a method something like this:

     public static void MyUglyRefMethod(Func<object> refGet, Action<object> refSet)
     {
       var read = refGet();
       var newValue = new object();
       refSet(newValue);
     }
    

    With this, we can now provide ref like semantics over MyClass:

     MyClass a = new MyClass();
     MyUglyRefMethod(() => a.Property, (newValue) => a.Property = newValue);
     Assert.IsNotNull(a.Property);
    

    But that is simply ugly as hell.

    It’s simpler to make a method to take a ref MyClass – and then it can write to any of the properties directly.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.