tags:

views:

212

answers:

1

Hi,

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

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.

Josh