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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:27:02+00:00 2026-05-23T01:27:02+00:00

Using Moq, RhinoMocks or a similar framework, is there a way to configure a

  • 0

Using Moq, RhinoMocks or a similar framework, is there a way to configure a mock to implement both get and set on all properties of an object even if the interface does not?

I can certainly mock the objects manually, but would prefer to use an isolation framework like Moq or RhinoMocks to avoid creating a bunch of boilerplate classes.

Here’s some sample code:

//interface to be mocked
public interface IMyObject
{
    string Property1 { set; }
}

//test method code
var mock = Moq.Mock<IMyObject>();

//...some code here to configure all properties on mock to have a get and set...

var mockObject = mock.Object;

ClassUnderTest obj = new ClassUnderTest(mockObject);
obj.MethodUnderTest();

Assert.IsTrue(!String.IsNullOrEmpty(mockObject.Property1));    

Running the code as is will throw an exception on mockObject.Property1 because the IMyObject.Property1 property lacks the get accessor.

Thanks, DanO

  • 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-23T01:27:02+00:00Added an answer on May 23, 2026 at 1:27 am

    Using Rhino Mocks, you could set an expectation for the property. This basically states that you expect the property to be set to a certain value. Using your example, an example test method body would be this:

    var mock = MockRepository.GenerateMock();
    mock.Expect(x => x.Property1 = “Test”);

    var classUnderTest = new ClassUnderTest(mock);
    classUnderTest.MethodUnderTest();
    
    mock.VerifyAllExpectations();
    

    This would check to see if Property1 had been set to “Test” at some point between the call to mock.Expect and the call to mock.MethodUnderTest (technically, Property1 could be set in the constructor of ClassUnderTest).

    In order to test that the property was set and ignore what it was actually set to, just chain IgnoreArguments to the return of the Expect call, like so:

    mock.Expect(x => x.Property1 = “Test1”).IgnoreArguments();

    One way to test more complex write-only properties is to use the GetArgumentsForCallsMadeOn method. This allows you to retrieve a list of the arguments passed to each “call” of the property. The code to do this would be like the following:

    var mock = MockRepository.GenerateMock();

    var classUnderTest = new ClassUnderTest(mock);
    classUnderTest.MethodUnderTest();
    
    //the argument in the Action is ignored, so just use null
    //Property1 is of type List<string>
    var arguments = mock.GetArgumentsForCallsMadeOn(x => x.Property1 = null);
    
    //arguments[0] contains the list of arguments for the first "call" of the
    //property the first index (0) of that would contain the first argument
    var firstCallArguments = arguments[0];
    var firstArgument = (List<string>)firstCallArguments[0];
    Assert.AreEqual(3, firstArgument.Count);
    

    I am sure that Moq has similar functionality if you’d like to use that, instead.

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

Sidebar

Related Questions

Using Moq I am mocking a property, Report TheReport { get; set; } on
I've been using Moq framework in c# for mocking in unit tests however there
I am using Moq - but could easily swap to another mock framework if
I want to mock only the GetValue method of the following class, using Moq:
I am using the Moq framework for unit testing and would like to be
[using Moq] I am trying to mock a concrete class and mock a virtual
We are using Moq as our mocking framework, the problem is that type that
I'm using MoQ to test some controllers I have. I'm not able to set
I am using Moq as my mocking framework and I need to test a
I'm using MOQ to mock a method call with an expected return list. My

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.