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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:44:58+00:00 2026-05-13T19:44:58+00:00

I have a method that i want to mock that takes an array as

  • 0

I have a method that i want to mock that takes an array as a argument. In a real call, the method would modify this array and the resultant array would be use further along in code.
What i tried to do was pass in array to the mocked method call, that had values that would be valid when the array is used again. However what i find is when the call is being made to the mocked method it doesn’t use the array i have specfied when setting up the mock, instead using the original array, Is there a way around this problem.

e.g

public interface ITest
{
    int Do(int[] values);
}

public void MyTest
{
    var testMock = new Mock<ITest>();
    int[] returnArray = new int[] {1,2,3};
    testMock.Setup(x => x.Do(returnArray)).Returns(0);
    MyTestObject obj = new MyTestObject();
    obj.TestFunction(testMock.Object);
}

public class MyTestObject
{
  .....
    public void TestFunction(ITest value)
    {
         int [] array = new int[3];
         value.Do(array);
         if (array[0] == 1) 

This is where my test falls down as array is still the null array declared two lines above and not the array i specified in the mocked method call. Hope this explains what i am trying to achieve and if so is there anyway of doing it. Would also be open to using RhinoMocks as well if it could be done that way.

Thank in advance,

Kev

  • 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-13T19:44:58+00:00Added an answer on May 13, 2026 at 7:44 pm

    The only thing you have done here is to set up an expectation that your Do method will be called with returnArray as parameter, and that it will return 0 as the result. What you want to do is to

    1. create a strict mock to better see what is being executed
    2. setup a call that expects your initial array, and then runs a delegate to set the values to 1, 2, 3

    using Rhino Mock this would look like this (using moq it will be equivalent):

    private delegate int DoDelegate(int[] values);
    
    [Test]
    public void MyTest()
    {
        var testMock = MockRepository.GenerateStrictMock<ITest>();
        testMock.Expect(x => x.Do(null))
            .IgnoreArguments()
            .Do(new DoDelegate(delegate(int[] values)
                                 {
                                     values[0] = 1;
                                     values[1] = 2;
                                     values[2] = 3;
                                     return 0;
                                 }));
    
        //Execution (use your real object here obviously
        int[] array = new int[3];
        testMock.Do(array);
        Assert.AreEqual(1, array[0]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I want to have a method that takes any kind of number, is
I have a method that contains an asynchronous call like this: public void MyMethod()
I have a method: myMethod() {} that I want to make accessible to javascript.
I have a method CreateAccount(...) that I want to unit test. Basically it creates
I have a method that returns a list of type string. I want to
I have a method running on the EDT and within that I want to
I have a method in a class in another file that I want to
Okay, so I want to have a generic method that can read data from
Basically, if I have a method (or several methods) that I want to expose
I have a method song_link that calls link_to internally. I want the caller to

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.