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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:18:32+00:00 2026-06-05T02:18:32+00:00

I am trying to test a class similar to the example below: public class

  • 0

I am trying to test a class similar to the example below:

public class Service : IService
{
    public string A(string input)
    {            
        int attemptCount = 5;
        while (attemptCount > 0)
        {
            try
            {
                return TryA(input);
            }
            catch (ArgumentOutOfRangeException)
            {
                attemptCount--;
                if (attemptCount == 0)
                {
                    throw;
                }
                // Attempt 5 more times
                Thread.Sleep(1000);                        
            }                    
        }
        throw new ArgumentOutOfRangeException();            
    }

    public string TryA(string input)
    {
    // try actions, if fail will throw ArgumentOutOfRangeException
    }
}

[TestMethod]
public void Makes_5_Attempts()
{
    //  Arrange
    var _service = MockRepository.GeneratePartialMock<Service>();
    _service.Expect(x=>x.TryA(Arg<string>.Is.Anything)).IgnoreArguments().Throw(new ArgumentOutOfRangeException());

    //  Act
    Action act = () => _service.A("");

    //  Assert
    //  Assert TryA is attempted to be called 5 times
    _service.AssertWasCalled(x => x.TryA(Arg<string>.Is.Anything), opt => opt.Repeat.Times(5));
    //  Assert the Exception is eventually thrown
    act.ShouldThrow<ArgumentOutOfRangeException>();
}

The partial mocking doesn’t seem to accept my expectation. When I run the test I receive an error about the input. When I debug, I see that the actual implementation of the method is being executed instead of the expectation.

Am I doing this test correctly? According to the documentation ( http://ayende.com/wiki/Rhino%20Mocks%20Partial%20Mocks.ashx ): “A partial mock will call the method defined on the class unless you define an expectation for that method. If you have defined an expectation, it will use the normal rules for 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-05T02:18:34+00:00Added an answer on June 5, 2026 at 2:18 am

    It’s is important to note that mocking frameworks like Rhinomocks, Moq and NSubstitute use a feature in .NET called DynamicProxy that dynamically generates a derived class of the mock in memory. Classes must:

    • be an interface; or
    • non-sealed class with parameterless constructor; or
    • derive from MarshalByRefObject (moq has moved away from this feature)

    Methods must be part of the interface or made virtual so that alternate behaviors can be substituted at runtime.

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

Sidebar

Related Questions

I'm trying to read a file in java: Public class Test{ public static void
I wrote a simple class method Buy.get_days(string) , and is trying to test it
I am trying to write a unit test for the following class: @Transactional public
Here's a very simple example of what I'm trying to get around: class Test(object):
I'm trying to test a class that takes a factory ( Func<T> ) and
I am trying to write a Unit test for a class that has several
I'm trying to call Error(My Test) in normal cpp class (Not a COM object,
I currently am using a data structures similar to the following: public class Individual
I am trying to implement a generic abstract class in my service layer. I
I have a class that relies on NSUserDefaults that I'm trying to unit-test and

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.