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

The Archive Base Latest Questions

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

I’m trying to test a class that instantiates objects dynamically and stores them in

  • 0

I’m trying to test a class that instantiates objects dynamically and stores them in a collection. Because I need to test logic that instantiates them and operations on those objects, I thought it would be a great idea to create a helper fake factory class that could be injected for testing. Here’s what I came up with

public class RhinoFakeFactory<TFakeable> where TFakeable : class
{
    public List<TFakeable> Fakes { get; set; }
    public FakeType FakeTypeToCreate { get; set; }

    public TFakeable GetFake()
    {
        TFakeable fake = default(TFakeable);

        switch (FakeTypeToCreate)
        {
            case FakeType.None:
            case FakeType.Stub:
                fake = MockRepository.GenerateStub<TFakeable>();
                break;
            case FakeType.DynamicMock:
                fake = MockRepository.GenerateMock<TFakeable>();
                break;
            case FakeType.DynamicMockWithRemoting:
                fake = MockRepository.GenerateDynamicMockWithRemoting<TFakeable>();
                break;
            case FakeType.StrickMock:
                fake = MockRepository.GenerateStrictMock<TFakeable>();
                break;
            case FakeType.StrickMockWithRemoting:
                fake = MockRepository.GenerateStrictMockWithRemoting<TFakeable>();
                break;
            case FakeType.PartialMock:
                fake = MockRepository.GeneratePartialMock<TFakeable>();
                break;
        }

        Fakes.Add(fake);
        return fake;
    }

    public RhinoFakeFactory()
    {
        Fakes = new List<TFakeable>();
    }
}

public enum FakeType
{
    None,
    Stub,
    DynamicMock,
    DynamicMockWithRemoting,
    StrickMock,
    StrickMockWithRemoting,
    PartialMock
}

After I wrote it, I then thought “I can inject this just fine but now I have to reference my test project and namespaces”. This doesn’t sit well with me. Maybe because I’m new to unit testing and trying to stick to convention as much as possible. From what I understand, the test code should be mostly transparent to code being tested so that could can keep as much focus and clarity on what it is trying to accomplish as possible.

Should I stop worrying and just use my fake factory? I would like to because it generates fakes as I need them for instances like the one I described above and because I can access the instantiated fake objects in the Fakes property to check their state. Is there something else implemented or possibly something in the Rhino Mocks framework that already takes care of this situation?

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

    It sounds like your class is instantiating objects dynamically (possibly within some logic construct like a switch statement?) and you want to allow for the testing of this with your FakeFactory?

    Using the FakeFactory and having your class under test know about test code is a big code smell, indicating overly tight coupling as well as introducing an unnecessary concern to your class (in fact, the dynamic instantiation is already an unnecessary concern).

    What you should do is introduce the factory pattern as the way you get your dynamic objects, and have an interface over your factory which allows you to replace it using RhinoMocks. Then when you configure your Mock objects, you can tell the factory to return another Mock as needed.

    So your class will end up looking something like this:

    public class ClassBeingTested
    {
        private IFactory _yourFactory;
    
        public ClassBeingTested(IFactory yourFactory)
        {
            _yourFactory = yourFactory;
        }
    
        public MethodWithDynamicInstantiation()
        {
            IClass = _yourFactory.GetClassDynamically(someparam);
        }
    }
    

    I’ve not used RhinoMocks in years so won’t attempt some code, but with this pattern you minimize the knowledge your class has about the dynamic instantiation logic while also providing the place to hook in the mocks of the dynamic objects and additionally removing any knowledge of testing code.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but

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.