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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:30:59+00:00 2026-05-11T13:30:59+00:00

I just switched to Moq and have run into a problem. I’m testing a

  • 0

I just switched to Moq and have run into a problem. I’m testing a method that creates a new instance of a business object, sets the properties of the object from user input values and calls a method (SaveCustomerContact ) to save the new object. The business object is passed as a ref argument because it goes through a remoting layer. I need to test that the object being passed to SaveCustomerContact has all of its properties set as expected, but because it is instantiated as new in the controller method I can’t seem to do so.

public void AddContact() {      var contact = new CustomerContact() { CustomerId = m_model.CustomerId };      contact.Name = m_model.CustomerContactName;     contact.PhoneNumber = m_model.PhoneNumber;     contact.FaxNumber = m_model.FaxNumber;     contact.Email = m_model.Email;     contact.ReceiveInvoiceFlag = m_model.ReceiveInvoiceFlag;     contact.ReceiveStatementFlag = m_model.ReceiveStatementFlag;     contact.ReceiveContractFlag = m_model.ReceiveContractFlag;     contact.EmailFlag = m_model.EmailFlag;     contact.FaxFlag = m_model.FaxFlag;     contact.PostalMailFlag = m_model.PostalMailFlag;     contact.CustomerLocationId = m_model.CustomerLocationId;      RemotingHandler.SaveCustomerContact( ref contact ); } 

Here’s the test:

[TestMethod()] public void AddContactTest() {      int customerId = 0;      string name = 'a';      var actual = new CustomerContact();      var expected = new CustomerContact() {         CustomerId = customerId,         Name = name     };      model.Setup( m => m.CustomerId ).Returns( customerId );     model.SetupProperty( m => model.CustomerContactName, name );     model.SetupProperty( m => m.PhoneNumber, string.Empty );     model.SetupProperty( m => m.FaxNumber, string.Empty );     model.SetupProperty( m => m.Email, string.Empty );     model.SetupProperty( m => m.ReceiveInvoiceFlag, false );     model.SetupProperty( m => m.ReceiveStatementFlag, false );     model.SetupProperty( m => m.ReceiveContractFlag, false );     model.SetupProperty( m => m.EmailFlag, false );     model.SetupProperty( m => m.FaxFlag, false );     model.SetupProperty( m => m.PostalMailFlag, false );     model.SetupProperty( m => m.CustomerLocationId, 0 );      remote         .Setup( r => r.SaveCustomerContact( ref actual ) )         .Callback( () => Assert.AreEqual( actual, expected ) );      target.AddContact();  } 

This is just the most recent of many attempts to get ahold of that parameter. For reference, the value of actual does not change from its initial (constructed) state.

Moving the Assert.AreEqual(expected, actual) after the target call fails. If I add .Verifiable() to the setup instead of the .CallBack and then call remote.Verify after the target (or, I assume, set the mock to strict) it always fails because the parameter I provide in the test is not the same instance as the one that is created in the controller method.

I’m using Moq 3.0.308.2. Any ideas on how to test this would be appreciated. Thanks!

  • 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. 2026-05-11T13:31:00+00:00Added an answer on May 11, 2026 at 1:31 pm

    I can’t offer you an exact solution, but an alternative would be to hide the pass-by-ref semantics behind an adapter, which takes the parameter by value and forwards it to the RemotingHandler. This would be easier to mock, and would remove the ‘ref’ wart from the interface (I am always suspicious of ref parameters 🙂 )

    EDIT:

    Or you could use a stub instead of a mock, for example:

    public class StubRemotingHandler : IRemotingHandler {     public CustomerContact savedContact;      public void SaveCustomerContact(ref CustomerContact contact)     {         savedContact = contact;     } } 

    You can now examine the saved object in your test:

    IRemotingHandler remote = new StubRemotingHandler(); ... //pass the stub to your object-under-test ... target.AddContact(); Assert.AreEqual(expected, remote.savedContact); 

    You also say in your comment:

    I’d hate to start a precedent of wrapping random bits of the backend so I can write tests more easily

    I think that’s exactly the precedent you need to set! If your code isn’t testable, you’re going to keep struggling to test it. Make it easier to test, and increase your coverage.

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

Sidebar

Ask A Question

Stats

  • Questions 96k
  • Answers 96k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want really ease the database load, take a… May 11, 2026 at 7:17 pm
  • Editorial Team
    Editorial Team added an answer You can specify which files to include in a jar… May 11, 2026 at 7:17 pm
  • Editorial Team
    Editorial Team added an answer I think you should start with MVVM first. http://msdn.microsoft.com/en-us/magazine/dd419663.aspx May 11, 2026 at 7:17 pm

Related Questions

I just switched to Moq and have run into a problem. I'm testing a
So firstly here's my query: ( NOTE:I know SELECT * is bad practice I
I just switched two projects over to fogbugz and so far I like it
I've just switched an application to use ar_mailer and when I run ar_sendmail (after
I've just switched from Windows to Mac and need to find a few tools

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.