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 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

I've been using Moq framework in c# for mocking in unit tests however there
Could someone explain why both tests using the latest versions of Moq and Rhino.Mocks
I am using Moq to mock my Repository layer so I can unit test.
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
I want to mock this interface using Moq IInterfaceToBeMocked { IEnumerable<Dude> SearchDudeByFilter(Expression<Func<Dude,bool>> filter); }
I'm getting the exception The type initializer for 'Moq.Mock`1' threw an exception. using Moq
I'm using Moq to create a Mock<HttpResponseBase> to test an FileResult I'm creating for
I have set a test using Moq 3.0. I need to test a method
I am trying to learn and implement TDD specifically using Moq and I have
I'm doing some unit testing, and mocking some properties using Moq . Now, this

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.