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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:38:59+00:00 2026-05-12T14:38:59+00:00

I have the following code(simplified). public class OrderProcessor { public virtual string PlaceOrder(string test)

  • 0

I have the following code(simplified).

public class OrderProcessor
{
    public virtual string PlaceOrder(string test)
    {
        OrderParser orderParser = new OrderParser();
        string tester =  orderParser.ParseOrder(test);
        return tester + " here" ;
    }

}

public class OrderParser
{

    public virtual string ParseOrder(string test)
    {
        if (!string.IsNullOrEmpty(test.Trim()))
        {
            if (test == "Test1")
                return "Test1";
            else
            {
                return "Hello";
            }
        }
        else
            return null;
    }
}

My test is as follows –

   public class OrderTest
   {
      public void TestParser()
      {
        // Arrange

        var client = MockRepository.GenerateMock<OrderProcessor>();
        var spec = MockRepository.GenerateStub<OrderParser>();

        spec.Stub(x => x.ParseOrder("test")).IgnoreArguments().Return("Test1");

        //How to pass spec to client so that it uses the same.

        }
    }

Now how do I test client so that it uses the mocked method from OrderParser.
I can mock the OrderParser but how do I pass that to the orderProcessor mocked class?

Please do let me know.

Thanks in advance.

  • 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-12T14:38:59+00:00Added an answer on May 12, 2026 at 2:38 pm

    I’m a little confused by your test since you are not really testing anything except that RhinoMocks works. You create two mocks and then do some assertions on them. You haven’t even tested your real classes.

    You need to do some dependency injection if you really want to get a good unit test. You can quickly refactor your code to use interfaces and dependency injection to make your test valid.

    Start by extracting an interface from your OrderParser class:

    public interface IOrderParser
    {
        String ParseOrder(String value);
    }
    

    Now make sure your OrderParser class implements that interface:

    public class OrderParser: IOrderParser{ ... }
    

    You can now refactor your OrderProcessor class to take in an instance of an IOrderParser object through its constructor. In this way you “inject” the dependency into the class.

    public class OrderProcessor
    {
        IOrderParser _orderParser;
    
        public OrderProcessor(IOrderParser orderParser)
        {
            _orderParser = orderParser;
        }
    
        public virtual string PlaceOrder(string val)
        {
            string tester =  _orderParser.ParseOrder(val);
            return tester + " here" ;
        }
    }
    

    In your test you only want to mock out the dependency and not the SUT (Subject Under Test). Your test would look something like this:

       public class OrderTest
       {
          public void TestParser()
          {
            // Arrange
    
            var spec = MockRepository.GenerateMock<IOrderParser>();
            var client = new OrderProcessor(spec);
    
            spec.Stub(x => x.ParseOrder("test")).IgnoreArguments().Return("Test1");
    
            //Act
            var s = client.PlaceOrder("Blah");
    
            //Assert
            Assert.AreEqual("Test1 Here", s);
    
            }
        }
    

    It is difficult for me to gauge what you are trying to do with your classes, but you should be able to get the idea from this. A few axioms to follow:

    1. Use interfaces and composition over inheritance
    2. Use dependency injection for external dependencies (inversion of control)
    3. Test a single unit, and mock its dependencies
    4. Only mock one level of dependencies. If you are testing class X which depends on Y which depends on Z, you should only be mocking Y and never Z.
    5. Always test behavior and never implementation details

    You seem to be on the right track, but need a little guidance. I would suggest reading material that Martin Fowler, and Bob Martin have to get up to speed.

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

Sidebar

Ask A Question

Stats

  • Questions 219k
  • Answers 219k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer if(drd.HasRows) { //.... } May 12, 2026 at 11:45 pm
  • Editorial Team
    Editorial Team added an answer Security Warning: Encryption without authentication is vulnerable to something called… May 12, 2026 at 11:45 pm
  • Editorial Team
    Editorial Team added an answer Use functools.wraps() to update the attributes of the decorator: from… May 12, 2026 at 11:45 pm

Related Questions

I have a method that takes an array of queries, and I need to
I'm using gcc 4.3.2. I have the following code (simplified): #include <cstdlib> template<int SIZE>
I am getting the following Compiler Warning: 'Resources.Foo.GetType()' hides inherited member 'object.GetType()'. Use the
With relationship aspects implemented in AspectJ I can associate objects of two types in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.